Skip to content

feat: update solutions to lcci problems: No.04.12,08.02 #4067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 36 additions & 42 deletions lcci/04.12.Paths with Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -145,9 +143,8 @@ class Solution {
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
unordered_map<long long, int> cnt;
cnt[0] = 1;
function<int(TreeNode*, long long)> dfs = [&](TreeNode* root, long long s) {
unordered_map<long long, int> cnt{{0, 1}};
auto dfs = [&](this auto&& dfs, TreeNode* root, long long s) -> int {
if (!root) {
return 0;
}
Expand Down Expand Up @@ -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)
}
}
```
Expand Down
79 changes: 37 additions & 42 deletions lcci/04.12.Paths with Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -158,9 +156,8 @@ class Solution {
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
unordered_map<long long, int> cnt;
cnt[0] = 1;
function<int(TreeNode*, long long)> dfs = [&](TreeNode* root, long long s) {
unordered_map<long long, int> cnt{{0, 1}};
auto dfs = [&](this auto&& dfs, TreeNode* root, long long s) -> int {
if (!root) {
return 0;
}
Expand Down Expand Up @@ -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)
}
}
```
Expand Down
7 changes: 3 additions & 4 deletions lcci/04.12.Paths with Sum/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
unordered_map<long long, int> cnt;
cnt[0] = 1;
function<int(TreeNode*, long long)> dfs = [&](TreeNode* root, long long s) {
unordered_map<long long, int> cnt{{0, 1}};
auto dfs = [&](this auto&& dfs, TreeNode* root, long long s) -> int {
if (!root) {
return 0;
}
Expand All @@ -26,4 +25,4 @@ class Solution {
};
return dfs(root, 0);
}
};
};
14 changes: 6 additions & 8 deletions lcci/04.12.Paths with Sum/Solution.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
64 changes: 31 additions & 33 deletions lcci/04.12.Paths with Sum/Solution.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
8 changes: 4 additions & 4 deletions lcci/05.03.Reverse Bits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions lcci/05.03.Reverse Bits/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions lcci/05.03.Reverse Bits/Solution.swift
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Loading