Skip to content

Commit f3bd387

Browse files
authored
feat: update solutions to lcci problems: No.04.12,08.02 (#4067)
1 parent 67e5a65 commit f3bd387

File tree

11 files changed

+134
-146
lines changed

11 files changed

+134
-146
lines changed

lcci/04.12.Paths with Sum/README.md

+36-42
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,13 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/04.12.Paths%20with%20
7070
```python
7171
# Definition for a binary tree node.
7272
# class TreeNode:
73-
# def __init__(self, x):
74-
# self.val = x
75-
# self.left = None
76-
# self.right = None
77-
78-
73+
# def __init__(self, val=0, left=None, right=None):
74+
# self.val = val
75+
# self.left = left
76+
# self.right = right
7977
class Solution:
80-
def pathSum(self, root: TreeNode, sum: int) -> int:
81-
def dfs(root: TreeNode, s: int):
78+
def pathSum(self, root: Optional[TreeNode], sum: int) -> int:
79+
def dfs(root: Optional[TreeNode], s: int) -> int:
8280
if root is None:
8381
return 0
8482
s += root.val
@@ -145,9 +143,8 @@ class Solution {
145143
class Solution {
146144
public:
147145
int pathSum(TreeNode* root, int sum) {
148-
unordered_map<long long, int> cnt;
149-
cnt[0] = 1;
150-
function<int(TreeNode*, long long)> dfs = [&](TreeNode* root, long long s) {
146+
unordered_map<long long, int> cnt{{0, 1}};
147+
auto dfs = [&](this auto&& dfs, TreeNode* root, long long s) -> int {
151148
if (!root) {
152149
return 0;
153150
}
@@ -285,43 +282,40 @@ impl Solution {
285282
#### Swift
286283

287284
```swift
288-
/* class TreeNode {
289-
* var val: Int
290-
* var left: TreeNode?
291-
* var right: TreeNode?
292-
*
293-
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
294-
* self.val = val
295-
* self.left = left
296-
* self.right = right
297-
* }
298-
* }
299-
*/
300-
285+
/**
286+
* Definition for a binary tree node.
287+
* public class TreeNode {
288+
* public var val: Int
289+
* public var left: TreeNode?
290+
* public var right: TreeNode?
291+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
292+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
293+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
294+
* self.val = val
295+
* self.left = left
296+
* self.right = right
297+
* }
298+
* }
299+
*/
301300
class Solution {
302-
private var cnt: [Int: Int] = [:]
303-
private var target: Int = 0
304-
305301
func pathSum(_ root: TreeNode?, _ sum: Int) -> Int {
306-
cnt[0] = 1
307-
target = sum
308-
return dfs(root, 0)
302+
var cnt: [Int: Int] = [0: 1]
309303

310-
}
304+
func dfs(_ root: TreeNode?, _ s: Int) -> Int {
305+
guard let root = root else { return 0 }
311306

312-
private func dfs(_ root: TreeNode?, _ s: Int) -> Int {
313-
guard let root = root else {
314-
return 0
315-
}
316-
let newSum = s + root.val
317-
let ans = cnt[newSum - target, default: 0]
307+
var s = s + root.val
308+
var ans = cnt[s - sum, default: 0]
318309

319-
cnt[newSum, default: 0] += 1
320-
let leftPaths = dfs(root.left, newSum)
321-
let rightPaths = dfs(root.right, newSum)
322-
cnt[newSum, default: 0] -= 1
310+
cnt[s, default: 0] += 1
311+
ans += dfs(root.left, s)
312+
ans += dfs(root.right, s)
313+
cnt[s, default: 0] -= 1
323314

324-
return ans + leftPaths + rightPaths
315+
return ans
316+
}
317+
318+
return dfs(root, 0)
325319
}
326320
}
327321
```

lcci/04.12.Paths with Sum/README_EN.md

+37-42
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,13 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is
8383
```python
8484
# Definition for a binary tree node.
8585
# class TreeNode:
86-
# def __init__(self, x):
87-
# self.val = x
88-
# self.left = None
89-
# self.right = None
90-
91-
86+
# def __init__(self, val=0, left=None, right=None):
87+
# self.val = val
88+
# self.left = left
89+
# self.right = right
9290
class Solution:
93-
def pathSum(self, root: TreeNode, sum: int) -> int:
94-
def dfs(root: TreeNode, s: int):
91+
def pathSum(self, root: Optional[TreeNode], sum: int) -> int:
92+
def dfs(root: Optional[TreeNode], s: int) -> int:
9593
if root is None:
9694
return 0
9795
s += root.val
@@ -158,9 +156,8 @@ class Solution {
158156
class Solution {
159157
public:
160158
int pathSum(TreeNode* root, int sum) {
161-
unordered_map<long long, int> cnt;
162-
cnt[0] = 1;
163-
function<int(TreeNode*, long long)> dfs = [&](TreeNode* root, long long s) {
159+
unordered_map<long long, int> cnt{{0, 1}};
160+
auto dfs = [&](this auto&& dfs, TreeNode* root, long long s) -> int {
164161
if (!root) {
165162
return 0;
166163
}
@@ -298,42 +295,40 @@ impl Solution {
298295
#### Swift
299296

300297
```swift
301-
/* class TreeNode {
302-
* var val: Int
303-
* var left: TreeNode?
304-
* var right: TreeNode?
305-
*
306-
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
307-
* self.val = val
308-
* self.left = left
309-
* self.right = right
310-
* }
311-
* }
312-
*/
313-
298+
/**
299+
* Definition for a binary tree node.
300+
* public class TreeNode {
301+
* public var val: Int
302+
* public var left: TreeNode?
303+
* public var right: TreeNode?
304+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
305+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
306+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
307+
* self.val = val
308+
* self.left = left
309+
* self.right = right
310+
* }
311+
* }
312+
*/
314313
class Solution {
315-
private var cnt: [Int: Int] = [:]
316-
private var target: Int = 0
317-
318314
func pathSum(_ root: TreeNode?, _ sum: Int) -> Int {
319-
cnt[0] = 1
320-
target = sum
321-
return dfs(root, 0)
322-
}
315+
var cnt: [Int: Int] = [0: 1]
323316

324-
private func dfs(_ root: TreeNode?, _ s: Int) -> Int {
325-
guard let root = root else {
326-
return 0
327-
}
328-
let newSum = s + root.val
329-
let ans = cnt[newSum - target, default: 0]
317+
func dfs(_ root: TreeNode?, _ s: Int) -> Int {
318+
guard let root = root else { return 0 }
319+
320+
var s = s + root.val
321+
var ans = cnt[s - sum, default: 0]
322+
323+
cnt[s, default: 0] += 1
324+
ans += dfs(root.left, s)
325+
ans += dfs(root.right, s)
326+
cnt[s, default: 0] -= 1
330327

331-
cnt[newSum, default: 0] += 1
332-
let leftPaths = dfs(root.left, newSum)
333-
let rightPaths = dfs(root.right, newSum)
334-
cnt[newSum, default: 0] -= 1
328+
return ans
329+
}
335330

336-
return ans + leftPaths + rightPaths
331+
return dfs(root, 0)
337332
}
338333
}
339334
```

lcci/04.12.Paths with Sum/Solution.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
class Solution {
1111
public:
1212
int pathSum(TreeNode* root, int sum) {
13-
unordered_map<long long, int> cnt;
14-
cnt[0] = 1;
15-
function<int(TreeNode*, long long)> dfs = [&](TreeNode* root, long long s) {
13+
unordered_map<long long, int> cnt{{0, 1}};
14+
auto dfs = [&](this auto&& dfs, TreeNode* root, long long s) -> int {
1615
if (!root) {
1716
return 0;
1817
}
@@ -26,4 +25,4 @@ class Solution {
2625
};
2726
return dfs(root, 0);
2827
}
29-
};
28+
};

lcci/04.12.Paths with Sum/Solution.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# Definition for a binary tree node.
22
# class TreeNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.left = None
6-
# self.right = None
7-
8-
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
97
class Solution:
10-
def pathSum(self, root: TreeNode, sum: int) -> int:
11-
def dfs(root: TreeNode, s: int):
8+
def pathSum(self, root: Optional[TreeNode], sum: int) -> int:
9+
def dfs(root: Optional[TreeNode], s: int) -> int:
1210
if root is None:
1311
return 0
1412
s += root.val
+31-33
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
1-
/* class TreeNode {
2-
* var val: Int
3-
* var left: TreeNode?
4-
* var right: TreeNode?
5-
*
6-
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
7-
* self.val = val
8-
* self.left = left
9-
* self.right = right
10-
* }
11-
* }
12-
*/
13-
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public var val: Int
5+
* public var left: TreeNode?
6+
* public var right: TreeNode?
7+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
8+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
9+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
10+
* self.val = val
11+
* self.left = left
12+
* self.right = right
13+
* }
14+
* }
15+
*/
1416
class Solution {
15-
private var cnt: [Int: Int] = [:]
16-
private var target: Int = 0
17-
1817
func pathSum(_ root: TreeNode?, _ sum: Int) -> Int {
19-
cnt[0] = 1
20-
target = sum
21-
return dfs(root, 0)
22-
}
18+
var cnt: [Int: Int] = [0: 1]
19+
20+
func dfs(_ root: TreeNode?, _ s: Int) -> Int {
21+
guard let root = root else { return 0 }
2322

24-
private func dfs(_ root: TreeNode?, _ s: Int) -> Int {
25-
guard let root = root else {
26-
return 0
23+
var s = s + root.val
24+
var ans = cnt[s - sum, default: 0]
25+
26+
cnt[s, default: 0] += 1
27+
ans += dfs(root.left, s)
28+
ans += dfs(root.right, s)
29+
cnt[s, default: 0] -= 1
30+
31+
return ans
2732
}
28-
let newSum = s + root.val
29-
let ans = cnt[newSum - target, default: 0]
30-
31-
cnt[newSum, default: 0] += 1
32-
let leftPaths = dfs(root.left, newSum)
33-
let rightPaths = dfs(root.right, newSum)
34-
cnt[newSum, default: 0] -= 1
35-
36-
return ans + leftPaths + rightPaths
33+
34+
return dfs(root, 0)
3735
}
38-
}
36+
}

lcci/05.03.Reverse Bits/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ function reverseBits(num: number): number {
134134
class Solution {
135135
func reverseBits(_ num: Int) -> Int {
136136
var ans = 0
137-
var countZeros = 0
137+
var cnt = 0
138138
var j = 0
139139

140140
for i in 0..<32 {
141-
countZeros += (num >> i & 1 ^ 1)
142-
while countZeros > 1 {
143-
countZeros -= (num >> j & 1 ^ 1)
141+
cnt += (num >> i & 1 ^ 1)
142+
while cnt > 1 {
143+
cnt -= (num >> j & 1 ^ 1)
144144
j += 1
145145
}
146146
ans = max(ans, i - j + 1)

lcci/05.03.Reverse Bits/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ function reverseBits(num: number): number {
142142
class Solution {
143143
func reverseBits(_ num: Int) -> Int {
144144
var ans = 0
145-
var countZeros = 0
145+
var cnt = 0
146146
var j = 0
147147

148148
for i in 0..<32 {
149-
countZeros += (num >> i & 1 ^ 1)
150-
while countZeros > 1 {
151-
countZeros -= (num >> j & 1 ^ 1)
149+
cnt += (num >> i & 1 ^ 1)
150+
while cnt > 1 {
151+
cnt -= (num >> j & 1 ^ 1)
152152
j += 1
153153
}
154154
ans = max(ans, i - j + 1)

lcci/05.03.Reverse Bits/Solution.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
func reverseBits(_ num: Int) -> Int {
33
var ans = 0
4-
var countZeros = 0
4+
var cnt = 0
55
var j = 0
66

77
for i in 0..<32 {
8-
countZeros += (num >> i & 1 ^ 1)
9-
while countZeros > 1 {
10-
countZeros -= (num >> j & 1 ^ 1)
8+
cnt += (num >> i & 1 ^ 1)
9+
while cnt > 1 {
10+
cnt -= (num >> j & 1 ^ 1)
1111
j += 1
1212
}
1313
ans = max(ans, i - j + 1)

0 commit comments

Comments
 (0)