Skip to content

Commit c3a745b

Browse files
committed
2024
1 parent 64a4629 commit c3a745b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1732
-1132
lines changed

cpp/.clang-format

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
BasedOnStyle: LLVM
2+
IndentWidth: 4
3+
---
4+
Language: Cpp
5+
# Force pointers to the type for C++.
6+
# AccessModifierOffset: -4
7+
DerivePointerAlignment: false
8+
PointerAlignment: Left

cpp/100.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class Solution {
1212
public:
1313
bool isSameTree(TreeNode *p, TreeNode *q) {
14-
if (p == NULL || q == NULL)
14+
if (p == nullptr || q == nullptr)
1515
return p == q;
1616
return p->val == q->val && isSameTree(p->left, q->left) &&
1717
isSameTree(p->right, q->right);

cpp/102.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
* };
1010
*/
1111
class Solution {
12-
public:
13-
vector<vector<int>> levelOrder(TreeNode *root) {
14-
vector<vector<int>> result;
15-
preOrder(0, root, result);
16-
return result;
17-
}
1812
void preOrder(int i, TreeNode *root, vector<vector<int>> &nums) {
19-
if (root == NULL)
13+
if (root == nullptr)
2014
return;
2115
if (nums.size() == i)
22-
nums.push_back(vector<int>());
16+
nums.push_back({});
2317
nums[i].push_back(root->val);
2418
preOrder(i + 1, root->left, nums);
2519
preOrder(i + 1, root->right, nums);
2620
}
21+
public:
22+
vector<vector<int>> levelOrder(TreeNode *root) {
23+
vector<vector<int>> result;
24+
preOrder(0, root, result);
25+
return result;
26+
}
2727
};

cpp/103.cpp

+39-39
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
// queue.cpp
21
/**
32
* Definition for a binary tree node.
43
* struct TreeNode {
54
* int val;
65
* TreeNode *left;
76
* TreeNode *right;
8-
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
10+
* right(right) {}
911
* };
1012
*/
1113
class Solution {
1214
public:
13-
vector<vector<int>> zigzagLevelOrder(TreeNode *root) {
14-
queue<TreeNode *> qe;
15-
vector<vector<int>> result;
16-
if (root == NULL)
17-
return result;
18-
qe.push(root);
19-
TreeNode *p;
20-
int level = 0, size;
21-
while (!qe.empty()) {
22-
result.push_back(vector<int>(qe.size()));
23-
size = qe.size();
24-
for (int i = 0; i < size; ++i) {
25-
p = qe.front();
26-
qe.pop();
27-
result[level][i] = p->val;
28-
if (p->left != NULL)
29-
qe.push(p->left);
30-
if (p->right != NULL)
31-
qe.push(p->right);
32-
}
33-
if (qe.empty())
34-
break;
35-
++level;
36-
result.push_back(vector<int>(qe.size()));
37-
for (int i = qe.size() - 1; i >= 0; --i) {
38-
p = qe.front();
39-
qe.pop();
40-
result[level][i] = p->val;
41-
if (p->left != NULL)
42-
qe.push(p->left);
43-
if (p->right != NULL)
44-
qe.push(p->right);
45-
}
46-
++level;
15+
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
16+
if (root == nullptr)
17+
return {};
18+
queue<TreeNode*> qe({root});
19+
vector<vector<int>> result;
20+
int level = 0, size;
21+
while (!qe.empty()) {
22+
result.push_back(vector<int>(qe.size()));
23+
size = qe.size();
24+
for (int i = 0; i < size; ++i) {
25+
auto p = qe.front();
26+
qe.pop();
27+
result[level][i] = p->val;
28+
if (p->left != nullptr)
29+
qe.push(p->left);
30+
if (p->right != nullptr)
31+
qe.push(p->right);
32+
}
33+
if (qe.empty())
34+
break;
35+
++level;
36+
result.push_back(vector<int>(qe.size()));
37+
for (int i = qe.size() - 1; i >= 0; --i) {
38+
auto p = qe.front();
39+
qe.pop();
40+
result[level][i] = p->val;
41+
if (p->left != nullptr)
42+
qe.push(p->left);
43+
if (p->right != nullptr)
44+
qe.push(p->right);
45+
}
46+
++level;
47+
}
48+
return result;
4749
}
48-
return result;
49-
}
50-
};
50+
};

cpp/104.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
// recursion-6ms.cpp
21
/**
32
* Definition for a binary tree node.
43
* struct TreeNode {
54
* int val;
65
* TreeNode *left;
76
* TreeNode *right;
8-
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
10+
* right(right) {}
911
* };
1012
*/
1113
class Solution {
1214
public:
13-
int maxDepth(TreeNode *root) {
14-
if (root == NULL)
15-
return 0;
16-
int a = 1 + maxDepth(root->left), b = 1 + maxDepth(root->right);
17-
if (a >= b)
18-
return a;
19-
return b;
20-
}
15+
int maxDepth(TreeNode* root) {
16+
if (root == nullptr)
17+
return 0;
18+
return 1 + max(maxDepth(root->left), maxDepth(root->right));
19+
}
2120
};

cpp/107.cpp

+19-16
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
1-
// dfsANDreverse.cpp
21
/**
32
* Definition for a binary tree node.
43
* struct TreeNode {
54
* int val;
65
* TreeNode *left;
76
* TreeNode *right;
8-
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
10+
* right(right) {}
911
* };
1012
*/
13+
1114
class Solution {
12-
vector<vector<int>> result;
15+
vector<vector<int>> result;
16+
void dfs(TreeNode* root, int depth) {
17+
if (root == nullptr)
18+
return;
19+
if (depth == result.size()) {
20+
result.push_back({});
21+
}
22+
result[depth].push_back(root->val);
23+
dfs(root->left, depth + 1);
24+
dfs(root->right, depth + 1);
25+
}
1326

1427
public:
15-
vector<vector<int>> levelOrderBottom(TreeNode *root) {
16-
dfs(root, 0);
17-
return vector<vector<int>>(result.rbegin(), result.rend());
18-
}
19-
void dfs(TreeNode *root, int depth) {
20-
if (root == NULL)
21-
return;
22-
if (depth == result.size()) {
23-
result.push_back(vector<int>());
28+
vector<vector<int>> levelOrderBottom(TreeNode* root) {
29+
dfs(root, 0);
30+
return {result.rbegin(), result.rend()};
2431
}
25-
result[depth].push_back(root->val);
26-
dfs(root->left, depth + 1);
27-
dfs(root->right, depth + 1);
28-
}
2932
};

cpp/108.cpp

+14-13
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
* };
1010
*/
1111
class Solution {
12-
public:
13-
TreeNode *sortedArrayToBST(vector<int> &nums) {
14-
return recursion(0, nums.size(), nums);
15-
}
16-
TreeNode *recursion(int low, int high, vector<int> &nums) {
17-
if (low >= high)
18-
return NULL;
19-
int mid = (low + high) / 2;
20-
TreeNode *p = new TreeNode(nums[mid]);
21-
p->left = recursion(low, mid, nums);
22-
p->right = recursion(mid + 1, high, nums);
23-
return p;
24-
}
12+
TreeNode* recursion(int low, int high, vector<int>& nums) {
13+
if (low >= high)
14+
return nullptr;
15+
int mid = (low + high) / 2;
16+
TreeNode* p = new TreeNode(nums[mid]);
17+
p->left = recursion(low, mid, nums);
18+
p->right = recursion(mid + 1, high, nums);
19+
return p;
20+
}
21+
22+
public:
23+
TreeNode* sortedArrayToBST(vector<int>& nums) {
24+
return recursion(0, nums.size(), nums);
25+
}
2526
};

cpp/112.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
* int val;
66
* TreeNode *left;
77
* TreeNode *right;
8-
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
99
* };
1010
*/
1111
class Solution {
12-
public:
13-
bool hasPathSum(TreeNode *root, int sum) {
14-
if (root == NULL)
15-
return 0;
16-
if (root->val == sum && root->left == NULL && root->right == NULL)
17-
return 1;
18-
return hasPathSum(root->left, sum - root->val) ||
19-
hasPathSum(root->right, sum - root->val);
20-
}
12+
public:
13+
bool hasPathSum(TreeNode* root, int sum) {
14+
if (root == nullptr)
15+
return 0;
16+
if (root->val == sum && root->left == nullptr && root->right == nullptr)
17+
return 1;
18+
return hasPathSum(root->left, sum - root->val) ||
19+
hasPathSum(root->right, sum - root->val);
20+
}
2121
};

cpp/113.cpp

+19-18
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@
55
* int val;
66
* TreeNode *left;
77
* TreeNode *right;
8-
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
99
* };
1010
*/
1111
class Solution {
12-
vector<vector<int>> result;
12+
vector<vector<int>> result;
13+
void hasSum(TreeNode* root, int sum, vector<int>& now) {
14+
if (root == nullptr)
15+
return;
16+
now.push_back(root->val);
17+
if (0 == sum - root->val && root->left == nullptr &&
18+
root->right == nullptr)
19+
result.push_back(now);
20+
hasSum(root->left, sum - root->val, now);
21+
hasSum(root->right, sum - root->val, now);
22+
now.pop_back();
23+
}
1324

14-
public:
15-
vector<vector<int>> pathSum(TreeNode *root, int sum) {
16-
vector<int> now;
17-
hasSum(root, sum, now);
18-
return result;
19-
}
20-
void hasSum(TreeNode *root, int sum, vector<int> &now) {
21-
if (root == NULL)
22-
return;
23-
now.push_back(root->val);
24-
if (0 == sum - root->val && root->left == NULL && root->right == NULL)
25-
result.push_back(now);
26-
hasSum(root->left, sum - root->val, now);
27-
hasSum(root->right, sum - root->val, now);
28-
now.pop_back();
29-
}
25+
public:
26+
vector<vector<int>> pathSum(TreeNode* root, int sum) {
27+
vector<int> now;
28+
hasSum(root, sum, now);
29+
return result;
30+
}
3031
};

cpp/118.cpp

+13-19
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
// iteration-3ms.cpp
1+
// iteration-0ms.cpp
22
class Solution {
3-
public:
4-
vector<vector<int>> generate(int numRows) {
5-
vector<vector<int>> result;
6-
if (numRows == 0)
7-
return result;
8-
result.push_back(vector<int>(1, 1));
9-
if (numRows == 1)
10-
return result;
11-
vector<int> a;
12-
for (int row = 1; row < numRows; ++row) {
13-
a.push_back(1);
14-
for (int i = 1; i < row; ++i)
15-
a.push_back(result[row - 1][i] + result[row - 1][i - 1]);
16-
a.push_back(1);
17-
result.push_back(a);
18-
a.clear();
3+
public:
4+
vector<vector<int>> generate(int numRows) {
5+
vector<vector<int>> result{{1}};
6+
vector<int> a;
7+
for (int row = 1; row < numRows; ++row) {
8+
a.push_back(1);
9+
for (int i = 1; i < row; ++i)
10+
a.push_back(result[row - 1][i] + result[row - 1][i - 1]);
11+
a.push_back(1);
12+
result.push_back(move(a));
13+
}
14+
return result;
1915
}
20-
return result;
21-
}
2216
};

cpp/120.cpp

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
// dp.cpp
22
class Solution {
3-
public:
4-
int minimumTotal(vector<vector<int>> &triangle) {
5-
int row = triangle.size();
6-
if (row == 0)
7-
return 0;
8-
vector<vector<int>> dp;
9-
dp.push_back(triangle[0]);
10-
for (int i = 0; i < row - 1; ++i) {
11-
dp.push_back(vector<int>(triangle[i + 1].size(), 0x7fffffff));
12-
for (int j = 0; j < triangle[i].size(); ++j) {
13-
for (int x = 0; x <= 1; ++x)
14-
if (j + x >= 0 && j + x < triangle[i + 1].size())
15-
dp[i + 1][j + x] =
16-
min(dp[i + 1][j + x], triangle[i + 1][j + x] + dp[i][j]);
17-
}
3+
public:
4+
int minimumTotal(vector<vector<int>>& triangle) {
5+
int row = triangle.size();
6+
vector<vector<int>> dp{triangle[0]};
7+
for (int i = 0; i < row - 1; ++i) {
8+
dp.push_back(vector<int>(triangle[i + 1].size(), 0x7fffffff));
9+
for (int j = 0; j < triangle[i].size(); ++j) {
10+
for (int x = 0; x <= 1; ++x)
11+
if (j + x >= 0 && j + x < triangle[i + 1].size())
12+
dp[i + 1][j + x] =
13+
min(dp[i + 1][j + x],
14+
triangle[i + 1][j + x] + dp[i][j]);
15+
}
16+
}
17+
return *min_element(dp.back().begin(), dp.back().end());
1818
}
19-
return *min_element(dp.back().begin(), dp.back().end());
20-
}
2119
};

0 commit comments

Comments
 (0)