Skip to content

Commit 8447415

Browse files
committed
feat: add swift solution2 implementation to lcof2 problem: No.088
1 parent 582af17 commit 8447415

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lcof2/剑指 Offer II 088. 爬楼梯的最少成本/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,23 @@ function minCostClimbingStairs(cost: number[]): number {
220220
}
221221
```
222222

223+
#### Swift
224+
225+
```swift
226+
class Solution {
227+
func minCostClimbingStairs(_ cost: [Int]) -> Int {
228+
var a = 0
229+
var b = 0
230+
for i in 1..<cost.count {
231+
let c = min(a + cost[i - 1], b + cost[i])
232+
a = b
233+
b = c
234+
}
235+
return b
236+
}
237+
}
238+
```
239+
223240
<!-- tabs:end -->
224241

225242
<!-- solution:end -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
func minCostClimbingStairs(_ cost: [Int]) -> Int {
3+
var a = 0
4+
var b = 0
5+
for i in 1..<cost.count {
6+
let c = min(a + cost[i - 1], b + cost[i])
7+
a = b
8+
b = c
9+
}
10+
return b
11+
}
12+
}

0 commit comments

Comments
 (0)