diff --git a/lcci/04.01.Route Between Nodes/README.md b/lcci/04.01.Route Between Nodes/README.md index 1846da42fc412..2e3b752f019a6 100644 --- a/lcci/04.01.Route Between Nodes/README.md +++ b/lcci/04.01.Route Between Nodes/README.md @@ -126,7 +126,7 @@ public: for (auto& e : graph) { g[e[0]].push_back(e[1]); } - function dfs = [&](int i) { + auto dfs = [&](this auto&& dfs, int i) -> bool { if (i == target) { return true; } diff --git a/lcci/04.01.Route Between Nodes/README_EN.md b/lcci/04.01.Route Between Nodes/README_EN.md index 5205c2f35b5a8..e32e55d20a57b 100644 --- a/lcci/04.01.Route Between Nodes/README_EN.md +++ b/lcci/04.01.Route Between Nodes/README_EN.md @@ -134,7 +134,7 @@ public: for (auto& e : graph) { g[e[0]].push_back(e[1]); } - function dfs = [&](int i) { + auto dfs = [&](this auto&& dfs, int i) -> bool { if (i == target) { return true; } diff --git a/lcci/04.01.Route Between Nodes/Solution.cpp b/lcci/04.01.Route Between Nodes/Solution.cpp index 915c3ca91ac20..0863689995365 100644 --- a/lcci/04.01.Route Between Nodes/Solution.cpp +++ b/lcci/04.01.Route Between Nodes/Solution.cpp @@ -6,7 +6,7 @@ class Solution { for (auto& e : graph) { g[e[0]].push_back(e[1]); } - function dfs = [&](int i) { + auto dfs = [&](this auto&& dfs, int i) -> bool { if (i == target) { return true; } @@ -23,4 +23,4 @@ class Solution { }; return dfs(start); } -}; \ No newline at end of file +}; diff --git a/lcci/04.02.Minimum Height Tree/README.md b/lcci/04.02.Minimum Height Tree/README.md index 3fccbc8f93760..f0c126c3a85ec 100644 --- a/lcci/04.02.Minimum Height Tree/README.md +++ b/lcci/04.02.Minimum Height Tree/README.md @@ -103,7 +103,7 @@ class Solution { class Solution { public: TreeNode* sortedArrayToBST(vector& nums) { - function dfs = [&](int l, int r) -> TreeNode* { + auto dfs = [&](this auto&& dfs, int l, int r) -> TreeNode* { if (l > r) { return nullptr; } diff --git a/lcci/04.02.Minimum Height Tree/README_EN.md b/lcci/04.02.Minimum Height Tree/README_EN.md index 40f629d61050d..84724a048b2f1 100644 --- a/lcci/04.02.Minimum Height Tree/README_EN.md +++ b/lcci/04.02.Minimum Height Tree/README_EN.md @@ -24,19 +24,19 @@ Given sorted array: [-10,-3,0,5,9], -One possible answer is: [0,-3,9,-10,null,5],which represents the following tree: +One possible answer is: [0,-3,9,-10,null,5],which represents the following tree: - 0 + 0 - / \ + / \ - -3 9 + -3 9 - / / + / / - -10 5 + -10 5 @@ -127,7 +127,7 @@ class Solution { class Solution { public: TreeNode* sortedArrayToBST(vector& nums) { - function dfs = [&](int l, int r) -> TreeNode* { + auto dfs = [&](this auto&& dfs, int l, int r) -> TreeNode* { if (l > r) { return nullptr; } diff --git a/lcci/04.02.Minimum Height Tree/Solution.cpp b/lcci/04.02.Minimum Height Tree/Solution.cpp index ecfbdddbae48a..66a66d1f27bb7 100644 --- a/lcci/04.02.Minimum Height Tree/Solution.cpp +++ b/lcci/04.02.Minimum Height Tree/Solution.cpp @@ -10,7 +10,7 @@ class Solution { public: TreeNode* sortedArrayToBST(vector& nums) { - function dfs = [&](int l, int r) -> TreeNode* { + auto dfs = [&](this auto&& dfs, int l, int r) -> TreeNode* { if (l > r) { return nullptr; } @@ -19,4 +19,4 @@ class Solution { }; return dfs(0, nums.size() - 1); } -}; \ No newline at end of file +}; diff --git a/solution/0100-0199/0131.Palindrome Partitioning/README.md b/solution/0100-0199/0131.Palindrome Partitioning/README.md index c07a31dee3ed0..b6d99b9b16013 100644 --- a/solution/0100-0199/0131.Palindrome Partitioning/README.md +++ b/solution/0100-0199/0131.Palindrome Partitioning/README.md @@ -149,14 +149,14 @@ public: } vector> ans; vector t; - function dfs = [&](int i) { + auto dfs = [&](this auto&& dfs, int i) -> void { if (i == n) { - ans.push_back(t); + ans.emplace_back(t); return; } for (int j = i; j < n; ++j) { if (f[i][j]) { - t.push_back(s.substr(i, j - i + 1)); + t.emplace_back(s.substr(i, j - i + 1)); dfs(j + 1); t.pop_back(); } diff --git a/solution/0100-0199/0131.Palindrome Partitioning/README_EN.md b/solution/0100-0199/0131.Palindrome Partitioning/README_EN.md index 07f5778b09663..269d65cc5fbfd 100644 --- a/solution/0100-0199/0131.Palindrome Partitioning/README_EN.md +++ b/solution/0100-0199/0131.Palindrome Partitioning/README_EN.md @@ -130,14 +130,14 @@ public: } vector> ans; vector t; - function dfs = [&](int i) { + auto dfs = [&](this auto&& dfs, int i) -> void { if (i == n) { - ans.push_back(t); + ans.emplace_back(t); return; } for (int j = i; j < n; ++j) { if (f[i][j]) { - t.push_back(s.substr(i, j - i + 1)); + t.emplace_back(s.substr(i, j - i + 1)); dfs(j + 1); t.pop_back(); } diff --git a/solution/0100-0199/0131.Palindrome Partitioning/Solution.cpp b/solution/0100-0199/0131.Palindrome Partitioning/Solution.cpp index e5397fdbf168c..d2189460eb537 100644 --- a/solution/0100-0199/0131.Palindrome Partitioning/Solution.cpp +++ b/solution/0100-0199/0131.Palindrome Partitioning/Solution.cpp @@ -11,14 +11,14 @@ class Solution { } vector> ans; vector t; - function dfs = [&](int i) { + auto dfs = [&](this auto&& dfs, int i) -> void { if (i == n) { - ans.push_back(t); + ans.emplace_back(t); return; } for (int j = i; j < n; ++j) { if (f[i][j]) { - t.push_back(s.substr(i, j - i + 1)); + t.emplace_back(s.substr(i, j - i + 1)); dfs(j + 1); t.pop_back(); } @@ -27,4 +27,4 @@ class Solution { dfs(0); return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1100-1199/1120.Maximum Average Subtree/README.md b/solution/1100-1199/1120.Maximum Average Subtree/README.md index c384c1043fd0c..5066218d54c67 100644 --- a/solution/1100-1199/1120.Maximum Average Subtree/README.md +++ b/solution/1100-1199/1120.Maximum Average Subtree/README.md @@ -160,7 +160,7 @@ class Solution { public: double maximumAverageSubtree(TreeNode* root) { double ans = 0; - function(TreeNode*)> dfs = [&](TreeNode* root) -> pair { + auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair { if (!root) { return {0, 0}; } diff --git a/solution/1100-1199/1120.Maximum Average Subtree/README_EN.md b/solution/1100-1199/1120.Maximum Average Subtree/README_EN.md index 8ebfa391c5277..e94b7e618f295 100644 --- a/solution/1100-1199/1120.Maximum Average Subtree/README_EN.md +++ b/solution/1100-1199/1120.Maximum Average Subtree/README_EN.md @@ -32,7 +32,7 @@ tags:
 Input: root = [5,6,1]
 Output: 6.00000
-Explanation: 
+Explanation:
 For the node with value = 5 we have an average of (5 + 6 + 1) / 3 = 4.
 For the node with value = 6 we have an average of 6 / 1 = 6.
 For the node with value = 1 we have an average of 1 / 1 = 1.
@@ -163,7 +163,7 @@ class Solution {
 public:
     double maximumAverageSubtree(TreeNode* root) {
         double ans = 0;
-        function(TreeNode*)> dfs = [&](TreeNode* root) -> pair {
+        auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair {
             if (!root) {
                 return {0, 0};
             }
diff --git a/solution/1100-1199/1120.Maximum Average Subtree/Solution.cpp b/solution/1100-1199/1120.Maximum Average Subtree/Solution.cpp
index 6a21aa9a1358d..8922bfd22342f 100644
--- a/solution/1100-1199/1120.Maximum Average Subtree/Solution.cpp	
+++ b/solution/1100-1199/1120.Maximum Average Subtree/Solution.cpp	
@@ -13,7 +13,7 @@ class Solution {
 public:
     double maximumAverageSubtree(TreeNode* root) {
         double ans = 0;
-        function(TreeNode*)> dfs = [&](TreeNode* root) -> pair {
+        auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair {
             if (!root) {
                 return {0, 0};
             }
@@ -27,4 +27,4 @@ class Solution {
         dfs(root);
         return ans;
     }
-};
\ No newline at end of file
+};
diff --git a/solution/1300-1399/1373.Maximum Sum BST in Binary Tree/README.md b/solution/1300-1399/1373.Maximum Sum BST in Binary Tree/README.md
index 679f50a3c2a93..a2d122cba229d 100644
--- a/solution/1300-1399/1373.Maximum Sum BST in Binary Tree/README.md	
+++ b/solution/1300-1399/1373.Maximum Sum BST in Binary Tree/README.md	
@@ -214,10 +214,9 @@ public:
     int maxSumBST(TreeNode* root) {
         int ans = 0;
         const int inf = 1 << 30;
-
-        function(TreeNode*)> dfs = [&](TreeNode* root) {
+        auto dfs = [&](this auto&& dfs, TreeNode* root) -> array {
             if (!root) {
-                return vector{1, inf, -inf, 0};
+                return {1, inf, -inf, 0};
             }
             auto l = dfs(root->left);
             auto r = dfs(root->right);
@@ -225,9 +224,9 @@ public:
             if (l[0] && r[0] && l[2] < v && v < r[1]) {
                 int s = l[3] + r[3] + v;
                 ans = max(ans, s);
-                return vector{1, min(l[1], v), max(r[2], v), s};
+                return {1, min(l[1], v), max(r[2], v), s};
             }
-            return vector(4);
+            return {0};
         };
         dfs(root);
         return ans;
diff --git a/solution/1300-1399/1373.Maximum Sum BST in Binary Tree/README_EN.md b/solution/1300-1399/1373.Maximum Sum BST in Binary Tree/README_EN.md
index 15bea428a53b1..62f5dded473a5 100644
--- a/solution/1300-1399/1373.Maximum Sum BST in Binary Tree/README_EN.md	
+++ b/solution/1300-1399/1373.Maximum Sum BST in Binary Tree/README_EN.md	
@@ -198,10 +198,9 @@ public:
     int maxSumBST(TreeNode* root) {
         int ans = 0;
         const int inf = 1 << 30;
-
-        function(TreeNode*)> dfs = [&](TreeNode* root) {
+        auto dfs = [&](this auto&& dfs, TreeNode* root) -> array {
             if (!root) {
-                return vector{1, inf, -inf, 0};
+                return {1, inf, -inf, 0};
             }
             auto l = dfs(root->left);
             auto r = dfs(root->right);
@@ -209,9 +208,9 @@ public:
             if (l[0] && r[0] && l[2] < v && v < r[1]) {
                 int s = l[3] + r[3] + v;
                 ans = max(ans, s);
-                return vector{1, min(l[1], v), max(r[2], v), s};
+                return {1, min(l[1], v), max(r[2], v), s};
             }
-            return vector(4);
+            return {0};
         };
         dfs(root);
         return ans;
diff --git a/solution/1300-1399/1373.Maximum Sum BST in Binary Tree/Solution.cpp b/solution/1300-1399/1373.Maximum Sum BST in Binary Tree/Solution.cpp
index f53230bf5e1cd..137c15f782014 100644
--- a/solution/1300-1399/1373.Maximum Sum BST in Binary Tree/Solution.cpp	
+++ b/solution/1300-1399/1373.Maximum Sum BST in Binary Tree/Solution.cpp	
@@ -14,10 +14,9 @@ class Solution {
     int maxSumBST(TreeNode* root) {
         int ans = 0;
         const int inf = 1 << 30;
-
-        function(TreeNode*)> dfs = [&](TreeNode* root) {
+        auto dfs = [&](this auto&& dfs, TreeNode* root) -> array {
             if (!root) {
-                return vector{1, inf, -inf, 0};
+                return {1, inf, -inf, 0};
             }
             auto l = dfs(root->left);
             auto r = dfs(root->right);
@@ -25,11 +24,11 @@ class Solution {
             if (l[0] && r[0] && l[2] < v && v < r[1]) {
                 int s = l[3] + r[3] + v;
                 ans = max(ans, s);
-                return vector{1, min(l[1], v), max(r[2], v), s};
+                return {1, min(l[1], v), max(r[2], v), s};
             }
-            return vector(4);
+            return {0};
         };
         dfs(root);
         return ans;
     }
-};
\ No newline at end of file
+};
diff --git a/solution/1500-1599/1559.Detect Cycles in 2D Grid/README.md b/solution/1500-1599/1559.Detect Cycles in 2D Grid/README.md
index e5049e8db2967..1cfee1a211d3c 100644
--- a/solution/1500-1599/1559.Detect Cycles in 2D Grid/README.md	
+++ b/solution/1500-1599/1559.Detect Cycles in 2D Grid/README.md	
@@ -452,7 +452,7 @@ public:
         int m = grid.size(), n = grid[0].size();
         vector> vis(m, vector(n));
         const vector dirs = {-1, 0, 1, 0, -1};
-        function dfs = [&](int x, int y, int px, int py) {
+        auto dfs = [&](this auto&& dfs, int x, int y, int px, int py) -> bool {
             vis[x][y] = true;
             for (int k = 0; k < 4; ++k) {
                 int nx = x + dirs[k], ny = y + dirs[k + 1];
diff --git a/solution/1500-1599/1559.Detect Cycles in 2D Grid/README_EN.md b/solution/1500-1599/1559.Detect Cycles in 2D Grid/README_EN.md
index f8ab55a046199..0fef522dab52e 100644
--- a/solution/1500-1599/1559.Detect Cycles in 2D Grid/README_EN.md	
+++ b/solution/1500-1599/1559.Detect Cycles in 2D Grid/README_EN.md	
@@ -452,7 +452,7 @@ public:
         int m = grid.size(), n = grid[0].size();
         vector> vis(m, vector(n));
         const vector dirs = {-1, 0, 1, 0, -1};
-        function dfs = [&](int x, int y, int px, int py) {
+        auto dfs = [&](this auto&& dfs, int x, int y, int px, int py) -> bool {
             vis[x][y] = true;
             for (int k = 0; k < 4; ++k) {
                 int nx = x + dirs[k], ny = y + dirs[k + 1];
diff --git a/solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution2.cpp b/solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution2.cpp
index 92575b73f06fc..28ea65909f08e 100644
--- a/solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution2.cpp	
+++ b/solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution2.cpp	
@@ -4,7 +4,7 @@ class Solution {
         int m = grid.size(), n = grid[0].size();
         vector> vis(m, vector(n));
         const vector dirs = {-1, 0, 1, 0, -1};
-        function dfs = [&](int x, int y, int px, int py) {
+        auto dfs = [&](this auto&& dfs, int x, int y, int px, int py) -> bool {
             vis[x][y] = true;
             for (int k = 0; k < 4; ++k) {
                 int nx = x + dirs[k], ny = y + dirs[k + 1];
diff --git a/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README.md b/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README.md
index e849cbcd0fa4e..3ddca17109029 100644
--- a/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README.md	
+++ b/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README.md	
@@ -160,13 +160,21 @@ class Solution {
 public:
     TreeNode* lowestCommonAncestor(TreeNode* root, vector& nodes) {
         unordered_set s;
-        for (auto node : nodes) s.insert(node->val);
-        function dfs = [&](TreeNode* root) -> TreeNode* {
-            if (!root || s.count(root->val)) return root;
+        for (auto node : nodes) {
+            s.insert(node->val);
+        }
+        auto dfs = [&](this auto&& dfs, TreeNode* root) -> TreeNode* {
+            if (!root || s.contains(root->val)) {
+                return root;
+            }
             auto left = dfs(root->left);
             auto right = dfs(root->right);
-            if (!left) return right;
-            if (!right) return left;
+            if (!left) {
+                return right;
+            }
+            if (!right) {
+                return left;
+            }
             return root;
         };
         return dfs(root);
diff --git a/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md b/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md
index 586320c75cc51..d735eb161fd8b 100644
--- a/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md	
+++ b/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md	
@@ -154,13 +154,21 @@ class Solution {
 public:
     TreeNode* lowestCommonAncestor(TreeNode* root, vector& nodes) {
         unordered_set s;
-        for (auto node : nodes) s.insert(node->val);
-        function dfs = [&](TreeNode* root) -> TreeNode* {
-            if (!root || s.count(root->val)) return root;
+        for (auto node : nodes) {
+            s.insert(node->val);
+        }
+        auto dfs = [&](this auto&& dfs, TreeNode* root) -> TreeNode* {
+            if (!root || s.contains(root->val)) {
+                return root;
+            }
             auto left = dfs(root->left);
             auto right = dfs(root->right);
-            if (!left) return right;
-            if (!right) return left;
+            if (!left) {
+                return right;
+            }
+            if (!right) {
+                return left;
+            }
             return root;
         };
         return dfs(root);
diff --git a/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/Solution.cpp b/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/Solution.cpp
index 76de9dcbbb50b..a83af3db68da4 100644
--- a/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/Solution.cpp	
+++ b/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/Solution.cpp	
@@ -13,15 +13,23 @@ class Solution {
 public:
     TreeNode* lowestCommonAncestor(TreeNode* root, vector& nodes) {
         unordered_set s;
-        for (auto node : nodes) s.insert(node->val);
-        function dfs = [&](TreeNode* root) -> TreeNode* {
-            if (!root || s.count(root->val)) return root;
+        for (auto node : nodes) {
+            s.insert(node->val);
+        }
+        auto dfs = [&](this auto&& dfs, TreeNode* root) -> TreeNode* {
+            if (!root || s.contains(root->val)) {
+                return root;
+            }
             auto left = dfs(root->left);
             auto right = dfs(root->right);
-            if (!left) return right;
-            if (!right) return left;
+            if (!left) {
+                return right;
+            }
+            if (!right) {
+                return left;
+            }
             return root;
         };
         return dfs(root);
     }
-};
\ No newline at end of file
+};