diff --git a/lcci/04.12.Paths with Sum/README.md b/lcci/04.12.Paths with Sum/README.md index 5ff30a2c961e5..6f0a4bcdfbffc 100644 --- a/lcci/04.12.Paths with Sum/README.md +++ b/lcci/04.12.Paths with Sum/README.md @@ -70,15 +70,13 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/04.12.Paths%20with%20 ```python # Definition for a binary tree node. # class TreeNode: -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - - +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - def pathSum(self, root: TreeNode, sum: int) -> int: - def dfs(root: TreeNode, s: int): + def pathSum(self, root: Optional[TreeNode], sum: int) -> int: + def dfs(root: Optional[TreeNode], s: int) -> int: if root is None: return 0 s += root.val @@ -145,9 +143,8 @@ class Solution { class Solution { public: int pathSum(TreeNode* root, int sum) { - unordered_map cnt; - cnt[0] = 1; - function dfs = [&](TreeNode* root, long long s) { + unordered_map cnt{{0, 1}}; + auto dfs = [&](this auto&& dfs, TreeNode* root, long long s) -> int { if (!root) { return 0; } @@ -285,43 +282,40 @@ impl Solution { #### Swift ```swift -/* class TreeNode { -* var val: Int -* var left: TreeNode? -* var right: TreeNode? -* -* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) { -* self.val = val -* self.left = left -* self.right = right -* } -* } -*/ - +/** + * Definition for a binary tree node. + * public class TreeNode { + * public var val: Int + * public var left: TreeNode? + * public var right: TreeNode? + * public init() { self.val = 0; self.left = nil; self.right = nil; } + * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } + * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { + * self.val = val + * self.left = left + * self.right = right + * } + * } + */ class Solution { - private var cnt: [Int: Int] = [:] - private var target: Int = 0 - func pathSum(_ root: TreeNode?, _ sum: Int) -> Int { - cnt[0] = 1 - target = sum - return dfs(root, 0) + var cnt: [Int: Int] = [0: 1] - } + func dfs(_ root: TreeNode?, _ s: Int) -> Int { + guard let root = root else { return 0 } - private func dfs(_ root: TreeNode?, _ s: Int) -> Int { - guard let root = root else { - return 0 - } - let newSum = s + root.val - let ans = cnt[newSum - target, default: 0] + var s = s + root.val + var ans = cnt[s - sum, default: 0] - cnt[newSum, default: 0] += 1 - let leftPaths = dfs(root.left, newSum) - let rightPaths = dfs(root.right, newSum) - cnt[newSum, default: 0] -= 1 + cnt[s, default: 0] += 1 + ans += dfs(root.left, s) + ans += dfs(root.right, s) + cnt[s, default: 0] -= 1 - return ans + leftPaths + rightPaths + return ans + } + + return dfs(root, 0) } } ``` diff --git a/lcci/04.12.Paths with Sum/README_EN.md b/lcci/04.12.Paths with Sum/README_EN.md index e2b797b3c05f5..179134478b763 100644 --- a/lcci/04.12.Paths with Sum/README_EN.md +++ b/lcci/04.12.Paths with Sum/README_EN.md @@ -83,15 +83,13 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is ```python # Definition for a binary tree node. # class TreeNode: -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - - +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - def pathSum(self, root: TreeNode, sum: int) -> int: - def dfs(root: TreeNode, s: int): + def pathSum(self, root: Optional[TreeNode], sum: int) -> int: + def dfs(root: Optional[TreeNode], s: int) -> int: if root is None: return 0 s += root.val @@ -158,9 +156,8 @@ class Solution { class Solution { public: int pathSum(TreeNode* root, int sum) { - unordered_map cnt; - cnt[0] = 1; - function dfs = [&](TreeNode* root, long long s) { + unordered_map cnt{{0, 1}}; + auto dfs = [&](this auto&& dfs, TreeNode* root, long long s) -> int { if (!root) { return 0; } @@ -298,42 +295,40 @@ impl Solution { #### Swift ```swift -/* class TreeNode { -* var val: Int -* var left: TreeNode? -* var right: TreeNode? -* -* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) { -* self.val = val -* self.left = left -* self.right = right -* } -* } -*/ - +/** + * Definition for a binary tree node. + * public class TreeNode { + * public var val: Int + * public var left: TreeNode? + * public var right: TreeNode? + * public init() { self.val = 0; self.left = nil; self.right = nil; } + * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } + * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { + * self.val = val + * self.left = left + * self.right = right + * } + * } + */ class Solution { - private var cnt: [Int: Int] = [:] - private var target: Int = 0 - func pathSum(_ root: TreeNode?, _ sum: Int) -> Int { - cnt[0] = 1 - target = sum - return dfs(root, 0) - } + var cnt: [Int: Int] = [0: 1] - private func dfs(_ root: TreeNode?, _ s: Int) -> Int { - guard let root = root else { - return 0 - } - let newSum = s + root.val - let ans = cnt[newSum - target, default: 0] + func dfs(_ root: TreeNode?, _ s: Int) -> Int { + guard let root = root else { return 0 } + + var s = s + root.val + var ans = cnt[s - sum, default: 0] + + cnt[s, default: 0] += 1 + ans += dfs(root.left, s) + ans += dfs(root.right, s) + cnt[s, default: 0] -= 1 - cnt[newSum, default: 0] += 1 - let leftPaths = dfs(root.left, newSum) - let rightPaths = dfs(root.right, newSum) - cnt[newSum, default: 0] -= 1 + return ans + } - return ans + leftPaths + rightPaths + return dfs(root, 0) } } ``` diff --git a/lcci/04.12.Paths with Sum/Solution.cpp b/lcci/04.12.Paths with Sum/Solution.cpp index 72e43d0c492c0..9095a6ab905d9 100644 --- a/lcci/04.12.Paths with Sum/Solution.cpp +++ b/lcci/04.12.Paths with Sum/Solution.cpp @@ -10,9 +10,8 @@ class Solution { public: int pathSum(TreeNode* root, int sum) { - unordered_map cnt; - cnt[0] = 1; - function dfs = [&](TreeNode* root, long long s) { + unordered_map cnt{{0, 1}}; + auto dfs = [&](this auto&& dfs, TreeNode* root, long long s) -> int { if (!root) { return 0; } @@ -26,4 +25,4 @@ class Solution { }; return dfs(root, 0); } -}; \ No newline at end of file +}; diff --git a/lcci/04.12.Paths with Sum/Solution.py b/lcci/04.12.Paths with Sum/Solution.py index 096e03e07552b..25e2179fe9ddc 100644 --- a/lcci/04.12.Paths with Sum/Solution.py +++ b/lcci/04.12.Paths with Sum/Solution.py @@ -1,14 +1,12 @@ # Definition for a binary tree node. # class TreeNode: -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None - - +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right class Solution: - def pathSum(self, root: TreeNode, sum: int) -> int: - def dfs(root: TreeNode, s: int): + def pathSum(self, root: Optional[TreeNode], sum: int) -> int: + def dfs(root: Optional[TreeNode], s: int) -> int: if root is None: return 0 s += root.val diff --git a/lcci/04.12.Paths with Sum/Solution.swift b/lcci/04.12.Paths with Sum/Solution.swift index cf1cbe9a5b95e..ad6fb0b161171 100644 --- a/lcci/04.12.Paths with Sum/Solution.swift +++ b/lcci/04.12.Paths with Sum/Solution.swift @@ -1,38 +1,36 @@ -/* class TreeNode { -* var val: Int -* var left: TreeNode? -* var right: TreeNode? -* -* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) { -* self.val = val -* self.left = left -* self.right = right -* } -* } -*/ - +/** + * Definition for a binary tree node. + * public class TreeNode { + * public var val: Int + * public var left: TreeNode? + * public var right: TreeNode? + * public init() { self.val = 0; self.left = nil; self.right = nil; } + * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } + * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { + * self.val = val + * self.left = left + * self.right = right + * } + * } + */ class Solution { - private var cnt: [Int: Int] = [:] - private var target: Int = 0 - func pathSum(_ root: TreeNode?, _ sum: Int) -> Int { - cnt[0] = 1 - target = sum - return dfs(root, 0) - } + var cnt: [Int: Int] = [0: 1] + + func dfs(_ root: TreeNode?, _ s: Int) -> Int { + guard let root = root else { return 0 } - private func dfs(_ root: TreeNode?, _ s: Int) -> Int { - guard let root = root else { - return 0 + var s = s + root.val + var ans = cnt[s - sum, default: 0] + + cnt[s, default: 0] += 1 + ans += dfs(root.left, s) + ans += dfs(root.right, s) + cnt[s, default: 0] -= 1 + + return ans } - let newSum = s + root.val - let ans = cnt[newSum - target, default: 0] - - cnt[newSum, default: 0] += 1 - let leftPaths = dfs(root.left, newSum) - let rightPaths = dfs(root.right, newSum) - cnt[newSum, default: 0] -= 1 - - return ans + leftPaths + rightPaths + + return dfs(root, 0) } -} \ No newline at end of file +} diff --git a/lcci/05.03.Reverse Bits/README.md b/lcci/05.03.Reverse Bits/README.md index cbec7bc2a611d..fad13598cbc1b 100644 --- a/lcci/05.03.Reverse Bits/README.md +++ b/lcci/05.03.Reverse Bits/README.md @@ -134,13 +134,13 @@ function reverseBits(num: number): number { class Solution { func reverseBits(_ num: Int) -> Int { var ans = 0 - var countZeros = 0 + var cnt = 0 var j = 0 for i in 0..<32 { - countZeros += (num >> i & 1 ^ 1) - while countZeros > 1 { - countZeros -= (num >> j & 1 ^ 1) + cnt += (num >> i & 1 ^ 1) + while cnt > 1 { + cnt -= (num >> j & 1 ^ 1) j += 1 } ans = max(ans, i - j + 1) diff --git a/lcci/05.03.Reverse Bits/README_EN.md b/lcci/05.03.Reverse Bits/README_EN.md index eb68a91114262..a9c88a668e959 100644 --- a/lcci/05.03.Reverse Bits/README_EN.md +++ b/lcci/05.03.Reverse Bits/README_EN.md @@ -142,13 +142,13 @@ function reverseBits(num: number): number { class Solution { func reverseBits(_ num: Int) -> Int { var ans = 0 - var countZeros = 0 + var cnt = 0 var j = 0 for i in 0..<32 { - countZeros += (num >> i & 1 ^ 1) - while countZeros > 1 { - countZeros -= (num >> j & 1 ^ 1) + cnt += (num >> i & 1 ^ 1) + while cnt > 1 { + cnt -= (num >> j & 1 ^ 1) j += 1 } ans = max(ans, i - j + 1) diff --git a/lcci/05.03.Reverse Bits/Solution.swift b/lcci/05.03.Reverse Bits/Solution.swift index da1c7fe2acf4a..0bd69d4893f51 100644 --- a/lcci/05.03.Reverse Bits/Solution.swift +++ b/lcci/05.03.Reverse Bits/Solution.swift @@ -1,13 +1,13 @@ class Solution { func reverseBits(_ num: Int) -> Int { var ans = 0 - var countZeros = 0 + var cnt = 0 var j = 0 for i in 0..<32 { - countZeros += (num >> i & 1 ^ 1) - while countZeros > 1 { - countZeros -= (num >> j & 1 ^ 1) + cnt += (num >> i & 1 ^ 1) + while cnt > 1 { + cnt -= (num >> j & 1 ^ 1) j += 1 } ans = max(ans, i - j + 1) diff --git a/lcci/08.02.Robot in a Grid/README.md b/lcci/08.02.Robot in a Grid/README.md index cc1bf9f600664..8b60835e73b0c 100644 --- a/lcci/08.02.Robot in a Grid/README.md +++ b/lcci/08.02.Robot in a Grid/README.md @@ -15,7 +15,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.02.Robot%20in%20a%

设想有个机器人坐在一个网格的左上角,网格 r 行 c 列。机器人只能向下或向右移动,但不能走到一些被禁止的网格(有障碍物)。设计一种算法,寻找机器人从左上角移动到右下角的路径。

+ ![](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcci/08.02.Robot%20in%20a%20Grid/images/robot_maze.png) +

网格中的障碍物和空位置分别用 10 来表示。

返回一条可行的路径,路径由经过的网格的行号和列号组成。左上角为 0 行 0 列。

示例 1:

@@ -26,7 +28,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.02.Robot%20in%20a%   [0,0,0] ] 输出: [[0,0],[0,1],[0,2],[1,2],[2,2]] -解释: +解释: 输入中标粗的位置即为输出表示的路径,即 0行0列(左上角) -> 0行1列 -> 0行2列 -> 1行2列 -> 2行2列(右下角)

说明:r 和 c 的值均不超过 100。

@@ -107,7 +109,7 @@ public: int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); vector> ans; - function dfs = [&](int i, int j) -> bool { + auto dfs = [&](this auto&& dfs, int i, int j) -> bool { if (i >= m || j >= n || obstacleGrid[i][j] == 1) { return false; } diff --git a/lcci/08.02.Robot in a Grid/README_EN.md b/lcci/08.02.Robot in a Grid/README_EN.md index 3ba14cfdcd585..bb37b1b1bf1d9 100644 --- a/lcci/08.02.Robot in a Grid/README_EN.md +++ b/lcci/08.02.Robot in a Grid/README_EN.md @@ -15,7 +15,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.02.Robot%20in%20a%

Imagine a robot sitting on the upper left corner of grid with r rows and c columns. The robot can only move in two directions, right and down, but certain cells are "off limits" such that the robot cannot step on them. Design an algorithm to find a path for the robot from the top left to the bottom right.

+ ![](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcci/08.02.Robot%20in%20a%20Grid/images/robot_maze.png) +

"off limits" and empty grid are represented by 1 and 0 respectively.

Return a valid path, consisting of row number and column number of grids in the path.

Example 1:

@@ -116,7 +118,7 @@ public: int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); vector> ans; - function dfs = [&](int i, int j) -> bool { + auto dfs = [&](this auto&& dfs, int i, int j) -> bool { if (i >= m || j >= n || obstacleGrid[i][j] == 1) { return false; } diff --git a/lcci/08.02.Robot in a Grid/Solution.cpp b/lcci/08.02.Robot in a Grid/Solution.cpp index e9657e05ef757..f7daef341d833 100644 --- a/lcci/08.02.Robot in a Grid/Solution.cpp +++ b/lcci/08.02.Robot in a Grid/Solution.cpp @@ -4,7 +4,7 @@ class Solution { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); vector> ans; - function dfs = [&](int i, int j) -> bool { + auto dfs = [&](this auto&& dfs, int i, int j) -> bool { if (i >= m || j >= n || obstacleGrid[i][j] == 1) { return false; } @@ -18,4 +18,4 @@ class Solution { }; return dfs(0, 0) ? ans : vector>(); } -}; \ No newline at end of file +};