Skip to content

Commit ac8a0c0

Browse files
authored
feat: add swift solution 2 implementation to lcof2 problem: No.098 (doocs#3504)
1 parent f160c3c commit ac8a0c0

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lcof2/剑指 Offer II 098. 路径的数目/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,24 @@ var uniquePaths = function (m, n) {
386386
};
387387
```
388388

389+
#### Swift
390+
391+
```swift
392+
class Solution {
393+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
394+
var dp = Array(repeating: Array(repeating: 1, count: n), count: m)
395+
396+
for i in 1..<m {
397+
for j in 1..<n {
398+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
399+
}
400+
}
401+
402+
return dp[m-1][n-1]
403+
}
404+
}
405+
```
406+
389407
<!-- tabs:end -->
390408

391409
<!-- solution:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
3+
var dp = Array(repeating: Array(repeating: 1, count: n), count: m)
4+
5+
for i in 1..<m {
6+
for j in 1..<n {
7+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
8+
}
9+
}
10+
11+
return dp[m-1][n-1]
12+
}
13+
}

0 commit comments

Comments
 (0)