Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 426f993

Browse files
feat: add swift implementation to lcci problem: No.04.12 (doocs#2662)
1 parent ca67b79 commit 426f993

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

‎lcci/04.12.Paths with Sum/README.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,48 @@ impl Solution {
257257
}
258258
```
259259

260+
```swift
261+
/* class TreeNode {
262+
* var val: Int
263+
* var left: TreeNode?
264+
* var right: TreeNode?
265+
*
266+
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
267+
* self.val = val
268+
* self.left = left
269+
* self.right = right
270+
* }
271+
* }
272+
*/
273+
274+
class Solution {
275+
private var cnt: [Int: Int] = [:]
276+
private var target: Int = 0
277+
278+
func pathSum(_ root: TreeNode?, _ sum: Int) -> Int {
279+
cnt[0] = 1
280+
target = sum
281+
return dfs(root, 0)
282+
283+
}
284+
285+
private func dfs(_ root: TreeNode?, _ s: Int) -> Int {
286+
guard let root = root else {
287+
return 0
288+
}
289+
let newSum = s + root.val
290+
let ans = cnt[newSum - target, default: 0]
291+
292+
cnt[newSum, default: 0] += 1
293+
let leftPaths = dfs(root.left, newSum)
294+
let rightPaths = dfs(root.right, newSum)
295+
cnt[newSum, default: 0] -= 1
296+
297+
return ans + leftPaths + rightPaths
298+
}
299+
}
300+
```
301+
260302
<!-- tabs:end -->
261303

262304
<!-- end -->

‎lcci/04.12.Paths with Sum/README_EN.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,47 @@ impl Solution {
269269
}
270270
```
271271

272+
```swift
273+
/* class TreeNode {
274+
* var val: Int
275+
* var left: TreeNode?
276+
* var right: TreeNode?
277+
*
278+
* init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
279+
* self.val = val
280+
* self.left = left
281+
* self.right = right
282+
* }
283+
* }
284+
*/
285+
286+
class Solution {
287+
private var cnt: [Int: Int] = [:]
288+
private var target: Int = 0
289+
290+
func pathSum(_ root: TreeNode?, _ sum: Int) -> Int {
291+
cnt[0] = 1
292+
target = sum
293+
return dfs(root, 0)
294+
}
295+
296+
private func dfs(_ root: TreeNode?, _ s: Int) -> Int {
297+
guard let root = root else {
298+
return 0
299+
}
300+
let newSum = s + root.val
301+
let ans = cnt[newSum - target, default: 0]
302+
303+
cnt[newSum, default: 0] += 1
304+
let leftPaths = dfs(root.left, newSum)
305+
let rightPaths = dfs(root.right, newSum)
306+
cnt[newSum, default: 0] -= 1
307+
308+
return ans + leftPaths + rightPaths
309+
}
310+
}
311+
```
312+
272313
<!-- tabs:end -->
273314

274315
<!-- end -->
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
14+
class Solution {
15+
private var cnt: [Int: Int] = [:]
16+
private var target: Int = 0
17+
18+
func pathSum(_ root: TreeNode?, _ sum: Int) -> Int {
19+
cnt[0] = 1
20+
target = sum
21+
return dfs(root, 0)
22+
}
23+
24+
private func dfs(_ root: TreeNode?, _ s: Int) -> Int {
25+
guard let root = root else {
26+
return 0
27+
}
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
37+
}
38+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /