Skip to content

Commit 2b30bd0

Browse files
authoredJan 4, 2025··
feat: update cpp solutions (#3923)
1 parent 7070b1f commit 2b30bd0

File tree

21 files changed

+85
-64
lines changed

21 files changed

+85
-64
lines changed
 

‎lcci/04.01.Route Between Nodes/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public:
126126
for (auto& e : graph) {
127127
g[e[0]].push_back(e[1]);
128128
}
129-
function<bool(int)> dfs = [&](int i) {
129+
auto dfs = [&](this auto&& dfs, int i) -> bool {
130130
if (i == target) {
131131
return true;
132132
}

‎lcci/04.01.Route Between Nodes/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public:
134134
for (auto& e : graph) {
135135
g[e[0]].push_back(e[1]);
136136
}
137-
function<bool(int)> dfs = [&](int i) {
137+
auto dfs = [&](this auto&& dfs, int i) -> bool {
138138
if (i == target) {
139139
return true;
140140
}

‎lcci/04.01.Route Between Nodes/Solution.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Solution {
66
for (auto& e : graph) {
77
g[e[0]].push_back(e[1]);
88
}
9-
function<bool(int)> dfs = [&](int i) {
9+
auto dfs = [&](this auto&& dfs, int i) -> bool {
1010
if (i == target) {
1111
return true;
1212
}
@@ -23,4 +23,4 @@ class Solution {
2323
};
2424
return dfs(start);
2525
}
26-
};
26+
};

‎lcci/04.02.Minimum Height Tree/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Solution {
103103
class Solution {
104104
public:
105105
TreeNode* sortedArrayToBST(vector<int>& nums) {
106-
function<TreeNode*(int, int)> dfs = [&](int l, int r) -> TreeNode* {
106+
auto dfs = [&](this auto&& dfs, int l, int r) -> TreeNode* {
107107
if (l > r) {
108108
return nullptr;
109109
}

‎lcci/04.02.Minimum Height Tree/README_EN.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ Given sorted array: [-10,-3,0,5,9],
2424

2525

2626

27-
One possible answer is: [0,-3,9,-10,null,5],which represents the following tree:
27+
One possible answer is: [0,-3,9,-10,null,5],which represents the following tree:
2828

2929

3030

31-
0
31+
0
3232

33-
/ \
33+
/ \
3434

35-
-3 9
35+
-3 9
3636

37-
/ /
37+
/ /
3838

39-
-10 5
39+
-10 5
4040

4141
</pre>
4242

@@ -127,7 +127,7 @@ class Solution {
127127
class Solution {
128128
public:
129129
TreeNode* sortedArrayToBST(vector<int>& nums) {
130-
function<TreeNode*(int, int)> dfs = [&](int l, int r) -> TreeNode* {
130+
auto dfs = [&](this auto&& dfs, int l, int r) -> TreeNode* {
131131
if (l > r) {
132132
return nullptr;
133133
}

‎lcci/04.02.Minimum Height Tree/Solution.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class Solution {
1111
public:
1212
TreeNode* sortedArrayToBST(vector<int>& nums) {
13-
function<TreeNode*(int, int)> dfs = [&](int l, int r) -> TreeNode* {
13+
auto dfs = [&](this auto&& dfs, int l, int r) -> TreeNode* {
1414
if (l > r) {
1515
return nullptr;
1616
}
@@ -19,4 +19,4 @@ class Solution {
1919
};
2020
return dfs(0, nums.size() - 1);
2121
}
22-
};
22+
};

‎solution/0100-0199/0131.Palindrome Partitioning/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ public:
149149
}
150150
vector<vector<string>> ans;
151151
vector<string> t;
152-
function<void(int)> dfs = [&](int i) {
152+
auto dfs = [&](this auto&& dfs, int i) -> void {
153153
if (i == n) {
154-
ans.push_back(t);
154+
ans.emplace_back(t);
155155
return;
156156
}
157157
for (int j = i; j < n; ++j) {
158158
if (f[i][j]) {
159-
t.push_back(s.substr(i, j - i + 1));
159+
t.emplace_back(s.substr(i, j - i + 1));
160160
dfs(j + 1);
161161
t.pop_back();
162162
}

‎solution/0100-0199/0131.Palindrome Partitioning/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,14 @@ public:
130130
}
131131
vector<vector<string>> ans;
132132
vector<string> t;
133-
function<void(int)> dfs = [&](int i) {
133+
auto dfs = [&](this auto&& dfs, int i) -> void {
134134
if (i == n) {
135-
ans.push_back(t);
135+
ans.emplace_back(t);
136136
return;
137137
}
138138
for (int j = i; j < n; ++j) {
139139
if (f[i][j]) {
140-
t.push_back(s.substr(i, j - i + 1));
140+
t.emplace_back(s.substr(i, j - i + 1));
141141
dfs(j + 1);
142142
t.pop_back();
143143
}

‎solution/0100-0199/0131.Palindrome Partitioning/Solution.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ class Solution {
1111
}
1212
vector<vector<string>> ans;
1313
vector<string> t;
14-
function<void(int)> dfs = [&](int i) {
14+
auto dfs = [&](this auto&& dfs, int i) -> void {
1515
if (i == n) {
16-
ans.push_back(t);
16+
ans.emplace_back(t);
1717
return;
1818
}
1919
for (int j = i; j < n; ++j) {
2020
if (f[i][j]) {
21-
t.push_back(s.substr(i, j - i + 1));
21+
t.emplace_back(s.substr(i, j - i + 1));
2222
dfs(j + 1);
2323
t.pop_back();
2424
}
@@ -27,4 +27,4 @@ class Solution {
2727
dfs(0);
2828
return ans;
2929
}
30-
};
30+
};

‎solution/1100-1199/1120.Maximum Average Subtree/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class Solution {
160160
public:
161161
double maximumAverageSubtree(TreeNode* root) {
162162
double ans = 0;
163-
function<pair<int, int>(TreeNode*)> dfs = [&](TreeNode* root) -> pair<int, int> {
163+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair<int, int> {
164164
if (!root) {
165165
return {0, 0};
166166
}

‎solution/1100-1199/1120.Maximum Average Subtree/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ tags:
3232
<pre>
3333
<strong>Input:</strong> root = [5,6,1]
3434
<strong>Output:</strong> 6.00000
35-
<strong>Explanation:</strong>
35+
<strong>Explanation:</strong>
3636
For the node with value = 5 we have an average of (5 + 6 + 1) / 3 = 4.
3737
For the node with value = 6 we have an average of 6 / 1 = 6.
3838
For the node with value = 1 we have an average of 1 / 1 = 1.
@@ -163,7 +163,7 @@ class Solution {
163163
public:
164164
double maximumAverageSubtree(TreeNode* root) {
165165
double ans = 0;
166-
function<pair<int, int>(TreeNode*)> dfs = [&](TreeNode* root) -> pair<int, int> {
166+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair<int, int> {
167167
if (!root) {
168168
return {0, 0};
169169
}

‎solution/1100-1199/1120.Maximum Average Subtree/Solution.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Solution {
1313
public:
1414
double maximumAverageSubtree(TreeNode* root) {
1515
double ans = 0;
16-
function<pair<int, int>(TreeNode*)> dfs = [&](TreeNode* root) -> pair<int, int> {
16+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair<int, int> {
1717
if (!root) {
1818
return {0, 0};
1919
}
@@ -27,4 +27,4 @@ class Solution {
2727
dfs(root);
2828
return ans;
2929
}
30-
};
30+
};

‎solution/1300-1399/1373.Maximum Sum BST in Binary Tree/README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,19 @@ public:
214214
int maxSumBST(TreeNode* root) {
215215
int ans = 0;
216216
const int inf = 1 << 30;
217-
218-
function<vector<int>(TreeNode*)> dfs = [&](TreeNode* root) {
217+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> array<int, 4> {
219218
if (!root) {
220-
return vector<int>{1, inf, -inf, 0};
219+
return {1, inf, -inf, 0};
221220
}
222221
auto l = dfs(root->left);
223222
auto r = dfs(root->right);
224223
int v = root->val;
225224
if (l[0] && r[0] && l[2] < v && v < r[1]) {
226225
int s = l[3] + r[3] + v;
227226
ans = max(ans, s);
228-
return vector<int>{1, min(l[1], v), max(r[2], v), s};
227+
return {1, min(l[1], v), max(r[2], v), s};
229228
}
230-
return vector<int>(4);
229+
return {0};
231230
};
232231
dfs(root);
233232
return ans;

‎solution/1300-1399/1373.Maximum Sum BST in Binary Tree/README_EN.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,19 @@ public:
198198
int maxSumBST(TreeNode* root) {
199199
int ans = 0;
200200
const int inf = 1 << 30;
201-
202-
function<vector<int>(TreeNode*)> dfs = [&](TreeNode* root) {
201+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> array<int, 4> {
203202
if (!root) {
204-
return vector<int>{1, inf, -inf, 0};
203+
return {1, inf, -inf, 0};
205204
}
206205
auto l = dfs(root->left);
207206
auto r = dfs(root->right);
208207
int v = root->val;
209208
if (l[0] && r[0] && l[2] < v && v < r[1]) {
210209
int s = l[3] + r[3] + v;
211210
ans = max(ans, s);
212-
return vector<int>{1, min(l[1], v), max(r[2], v), s};
211+
return {1, min(l[1], v), max(r[2], v), s};
213212
}
214-
return vector<int>(4);
213+
return {0};
215214
};
216215
dfs(root);
217216
return ans;

‎solution/1300-1399/1373.Maximum Sum BST in Binary Tree/Solution.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,21 @@ class Solution {
1414
int maxSumBST(TreeNode* root) {
1515
int ans = 0;
1616
const int inf = 1 << 30;
17-
18-
function<vector<int>(TreeNode*)> dfs = [&](TreeNode* root) {
17+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> array<int, 4> {
1918
if (!root) {
20-
return vector<int>{1, inf, -inf, 0};
19+
return {1, inf, -inf, 0};
2120
}
2221
auto l = dfs(root->left);
2322
auto r = dfs(root->right);
2423
int v = root->val;
2524
if (l[0] && r[0] && l[2] < v && v < r[1]) {
2625
int s = l[3] + r[3] + v;
2726
ans = max(ans, s);
28-
return vector<int>{1, min(l[1], v), max(r[2], v), s};
27+
return {1, min(l[1], v), max(r[2], v), s};
2928
}
30-
return vector<int>(4);
29+
return {0};
3130
};
3231
dfs(root);
3332
return ans;
3433
}
35-
};
34+
};

‎solution/1500-1599/1559.Detect Cycles in 2D Grid/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public:
452452
int m = grid.size(), n = grid[0].size();
453453
vector<vector<bool>> vis(m, vector<bool>(n));
454454
const vector<int> dirs = {-1, 0, 1, 0, -1};
455-
function<bool(int, int, int, int)> dfs = [&](int x, int y, int px, int py) {
455+
auto dfs = [&](this auto&& dfs, int x, int y, int px, int py) -> bool {
456456
vis[x][y] = true;
457457
for (int k = 0; k < 4; ++k) {
458458
int nx = x + dirs[k], ny = y + dirs[k + 1];

‎solution/1500-1599/1559.Detect Cycles in 2D Grid/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public:
452452
int m = grid.size(), n = grid[0].size();
453453
vector<vector<bool>> vis(m, vector<bool>(n));
454454
const vector<int> dirs = {-1, 0, 1, 0, -1};
455-
function<bool(int, int, int, int)> dfs = [&](int x, int y, int px, int py) {
455+
auto dfs = [&](this auto&& dfs, int x, int y, int px, int py) -> bool {
456456
vis[x][y] = true;
457457
for (int k = 0; k < 4; ++k) {
458458
int nx = x + dirs[k], ny = y + dirs[k + 1];

‎solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Solution {
44
int m = grid.size(), n = grid[0].size();
55
vector<vector<bool>> vis(m, vector<bool>(n));
66
const vector<int> dirs = {-1, 0, 1, 0, -1};
7-
function<bool(int, int, int, int)> dfs = [&](int x, int y, int px, int py) {
7+
auto dfs = [&](this auto&& dfs, int x, int y, int px, int py) -> bool {
88
vis[x][y] = true;
99
for (int k = 0; k < 4; ++k) {
1010
int nx = x + dirs[k], ny = y + dirs[k + 1];

‎solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README.md

+13-5
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,21 @@ class Solution {
160160
public:
161161
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*>& nodes) {
162162
unordered_set<int> s;
163-
for (auto node : nodes) s.insert(node->val);
164-
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
165-
if (!root || s.count(root->val)) return root;
163+
for (auto node : nodes) {
164+
s.insert(node->val);
165+
}
166+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> TreeNode* {
167+
if (!root || s.contains(root->val)) {
168+
return root;
169+
}
166170
auto left = dfs(root->left);
167171
auto right = dfs(root->right);
168-
if (!left) return right;
169-
if (!right) return left;
172+
if (!left) {
173+
return right;
174+
}
175+
if (!right) {
176+
return left;
177+
}
170178
return root;
171179
};
172180
return dfs(root);

‎solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md

+13-5
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,21 @@ class Solution {
154154
public:
155155
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*>& nodes) {
156156
unordered_set<int> s;
157-
for (auto node : nodes) s.insert(node->val);
158-
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
159-
if (!root || s.count(root->val)) return root;
157+
for (auto node : nodes) {
158+
s.insert(node->val);
159+
}
160+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> TreeNode* {
161+
if (!root || s.contains(root->val)) {
162+
return root;
163+
}
160164
auto left = dfs(root->left);
161165
auto right = dfs(root->right);
162-
if (!left) return right;
163-
if (!right) return left;
166+
if (!left) {
167+
return right;
168+
}
169+
if (!right) {
170+
return left;
171+
}
164172
return root;
165173
};
166174
return dfs(root);

‎solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/Solution.cpp

+14-6
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ class Solution {
1313
public:
1414
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*>& nodes) {
1515
unordered_set<int> s;
16-
for (auto node : nodes) s.insert(node->val);
17-
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
18-
if (!root || s.count(root->val)) return root;
16+
for (auto node : nodes) {
17+
s.insert(node->val);
18+
}
19+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> TreeNode* {
20+
if (!root || s.contains(root->val)) {
21+
return root;
22+
}
1923
auto left = dfs(root->left);
2024
auto right = dfs(root->right);
21-
if (!left) return right;
22-
if (!right) return left;
25+
if (!left) {
26+
return right;
27+
}
28+
if (!right) {
29+
return left;
30+
}
2331
return root;
2432
};
2533
return dfs(root);
2634
}
27-
};
35+
};

0 commit comments

Comments
 (0)
Please sign in to comment.