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 399f853

Browse files
feat: add swift implementation to lcof2 problem: No.099 (doocs#3505)
1 parent ac8a0c0 commit 399f853

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

‎lcof2/剑指 Offer II 099. 最小路径之和/README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,34 @@ public class Solution {
202202
}
203203
```
204204

205+
#### Swift
206+
207+
```swift
208+
class Solution {
209+
func minPathSum(_ grid: [[Int]]) -> Int {
210+
let m = grid.count
211+
let n = grid[0].count
212+
var dp = grid
213+
214+
for i in 1..<m {
215+
dp[i][0] += dp[i-1][0]
216+
}
217+
218+
for j in 1..<n {
219+
dp[0][j] += dp[0][j-1]
220+
}
221+
222+
for i in 1..<m {
223+
for j in 1..<n {
224+
dp[i][j] += min(dp[i-1][j], dp[i][j-1])
225+
}
226+
}
227+
228+
return dp[m-1][n-1]
229+
}
230+
}
231+
```
232+
205233
<!-- tabs:end -->
206234

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

0 commit comments

Comments
(0)

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