File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed
lcof2/剑指 Offer II 098. 路径的数目 Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -247,6 +247,30 @@ var uniquePaths = function (m, n) {
247
247
};
248
248
```
249
249
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
+
250
274
<!-- tabs: end -->
251
275
252
276
<!-- solution: end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments