Skip to content

Commit 411b6bf

Browse files
authored
feat: add swift solution2 implementation to lcof2 problem: No.088 (#3474)
1 parent 2dd8ca2 commit 411b6bf

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,23 @@ function minCostClimbingStairs(cost: number[]): number {
235235
}
236236
```
237237

238+
#### Swift
239+
240+
```swift
241+
class Solution {
242+
func minCostClimbingStairs(_ cost: [Int]) -> Int {
243+
var a = 0
244+
var b = 0
245+
for i in 1..<cost.count {
246+
let c = min(a + cost[i - 1], b + cost[i])
247+
a = b
248+
b = c
249+
}
250+
return b
251+
}
252+
}
253+
```
254+
238255
<!-- tabs:end -->
239256

240257
<!-- solution:end -->
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)