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 f160c3c

Browse files
feat: add swift implementation to lcof2 problem: No.098 (doocs#3503)
1 parent dd4898a commit f160c3c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,30 @@ var uniquePaths = function (m, n) {
247247
};
248248
```
249249

250+
#### Swift
251+
252+
```swift
253+
class Solution {
254+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
255+
var dp = Array(repeating: Array(repeating: 0, count: n), count: m)
256+
dp[0][0] = 1
257+
258+
for i in 0..<m {
259+
for j in 0..<n {
260+
if i > 0 {
261+
dp[i][j] += dp[i - 1][j]
262+
}
263+
if j > 0 {
264+
dp[i][j] += dp[i][j - 1]
265+
}
266+
}
267+
}
268+
269+
return dp[m - 1][n - 1]
270+
}
271+
}
272+
```
273+
250274
<!-- tabs:end -->
251275

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

0 commit comments

Comments
(0)

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