Skip to content

Commit ab1fab9

Browse files
committed
重构
1 parent 0fb372f commit ab1fab9

File tree

7 files changed

+35
-26
lines changed

7 files changed

+35
-26
lines changed

C++/LeetCodet题解(C++版).pdf

290 Bytes
Binary file not shown.

C++/chapBFS.tex

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,9 @@ \subsubsection{代码}
156156
while (!current.empty() && !found) {
157157
++level;
158158
// 先将本层全部置为已访问,防止同层之间互相指向
159-
for (auto iter = current.begin(); iter != current.end(); ++iter)
160-
visited.insert(*iter);
161-
for (auto iter = current.begin(); iter != current.end(); ++iter) {
162-
//while (!current.empty()) {
163-
const string word = *iter;
164-
159+
for (auto word : current)
160+
visited.insert(word);
161+
for (auto word : current) {
165162
for (size_t i = 0; i < word.size(); ++i) {
166163
string new_word = word;
167164
for (char c = 'a'; c <= 'z'; ++c) {
@@ -202,9 +199,8 @@ \subsubsection{代码}
202199
result.push_back(path);
203200
reverse(result.back().begin(), result.back().end());
204201
} else {
205-
for (auto iter = father[word].begin(); iter != father[word].end();
206-
++iter) {
207-
buildPath(father, path, start, *iter, result);
202+
for (auto f : father[word]) {
203+
buildPath(father, path, start, f, result);
208204
}
209205
}
210206
path.pop_back();

C++/chapBruteforce.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ \subsubsection{代码}
235235
sort(S.begin(), S.end()); // 本题对顺序有要求,需要排序
236236
vector<int> count(S.back() - S.front() + 1, 0);
237237
// 计算所有元素的个数
238-
for(auto i = S.begin(); i != S.end(); i++) {
239-
count[*i - S[0]]++;
238+
for (auto i : S) {
239+
count[i - S[0]]++;
240240
}
241241

242242
// 每个元素选择了多少个

C++/chapGreedy.tex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ \subsubsection{分析}
165165

166166
\subsubsection{代码}
167167
\begin{Code}
168+
// LeetCode, Best Time to Buy and Sell Stock
168169
class Solution {
169170
public:
170171
int maxProfit(vector<int> &prices) {
@@ -206,6 +207,7 @@ \subsubsection{分析}
206207

207208
\subsubsection{代码}
208209
\begin{Code}
210+
// LeetCode, Best Time to Buy and Sell Stock II
209211
class Solution {
210212
public:
211213
int maxProfit(vector<int> &prices) {

C++/chapImplement.tex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ \subsubsection{分析}
2727
\subsubsection{代码}
2828
\begin{Code}
2929
//LeetCode, Two Sum
30-
// 方法1:暴力,O(n^2)
3130
// 方法2:hash。用一个哈希表,存储每个数对应的下标,复杂度O(n),代码如下,
3231
class Solution {
3332
public:
@@ -571,7 +570,7 @@ \subsubsection{分析}
571570
\subsubsection{代码}
572571

573572
\begin{Code}
574-
// LeetCode, Pascal's Triangle
573+
// LeetCode, Pascal's Triangle II
575574
// 滚动数组
576575
class Solution {
577576
public:

C++/chapString.tex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ \subsubsection{代码}
7878
// 暴力解法,复杂度O(N*M)
7979
class Solution {
8080
public:
81-
char *strstr(const char *haystack, const char *needle) {
81+
char *strStr(const char *haystack, const char *needle) {
8282
// if needle is empty return the full string
8383
if (!*needle) return (char*) haystack;
8484

@@ -271,11 +271,23 @@ \subsubsection{代码}
271271

272272
\begin{Code}
273273
// LeetCode, Longest Palindromic Substring
274+
// 备忘录法,TLE
275+
typedef string::const_iterator Iterator;
276+
277+
namespace std {
278+
template<>
279+
struct hash<pair<Iterator, Iterator>> {
280+
size_t operator()(pair<Iterator, Iterator> const& p) const {
281+
return ((size_t) &(*p.first)) ^ ((size_t) &(*p.second));
282+
}
283+
};
284+
}
285+
274286
class Solution {
275287
public:
276288
string longestPalindrome(string const& s) {
277289
cache.clear();
278-
return cachedLongestPalindrome(begin(s), end(s));
290+
return cachedLongestPalindrome(s.begin(), s.end());
279291
}
280292

281293
private:

C++/chapTree.tex

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ \subsubsection{代码}
153153
}
154154

155155
vector<vector<int> > ret;
156-
for(auto iter = tmp_result.begin(); iter != tmp_result.end(); ++iter) {
157-
ret.push_back(*iter);
156+
for (auto e : tmp_result) {
157+
ret.push_back(e);
158158
}
159159
return ret;
160160
}
@@ -524,7 +524,7 @@ \subsubsection{代码}
524524
bool isSymmetric(TreeNode *left, TreeNode *right) {
525525
if (!left && !right) return true; // 终止条件
526526
if (!left || !right) return false; // 终止条件
527-
return left->val != right->val // 三方合并
527+
return left->val == right->val // 三方合并
528528
&& isSymmetric(left->left, right->right)
529529
&& isSymmetric(left->right, right->left);
530530
}
@@ -878,11 +878,11 @@ \subsubsection{代码}
878878
for (int k = start; k <= end; k++) {
879879
vector<TreeNode*> leftSubs = generate(start, k - 1);
880880
vector<TreeNode*> rightSubs = generate(k + 1, end);
881-
for (auto i = leftSubs.begin(); i < leftSubs.end(); i++) {
882-
for (auto j = rightSubs.begin(); j < rightSubs.end(); j++) {
881+
for (auto i : leftSubs) {
882+
for (auto j : rightSubs) {
883883
TreeNode *node = new TreeNode(k);
884-
node->left = *i;
885-
node->right = *j;
884+
node->left = i;
885+
node->right = j;
886886
subTree.push_back(node);
887887
}
888888
}
@@ -1483,20 +1483,20 @@ \subsubsection{代码}
14831483
class Solution {
14841484
public:
14851485
int maxPathSum(TreeNode *root) {
1486-
max = INT_MIN;
1486+
max_sum = INT_MIN;
14871487
dfs(root);
1488-
return max;
1488+
return max_sum;
14891489
}
14901490
private:
1491-
int max;
1491+
int max_sum;
14921492
int dfs(const TreeNode *root) {
14931493
if (root == nullptr) return 0;
14941494
int l = dfs(root->left);
14951495
int r = dfs(root->right);
14961496
int sum = root->val;
14971497
if (l > 0) sum += l;
14981498
if (r > 0) sum += r;
1499-
max = max(max, sum);
1499+
max_sum = max(max_sum, sum);
15001500
return max(r, l) > 0 ? max(r, l) + root->val : root->val;
15011501
}
15021502
};

0 commit comments

Comments
 (0)