mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #208 from reoaah/master
添加 0509.斐波那契数 0746.使用最小花费爬楼梯 Go 版本
This commit is contained in:
@ -207,6 +207,19 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func fib(n int) int {
|
||||||
|
if n < 2 {
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
a, b, c := 0, 1, 0
|
||||||
|
for i := 1; i < n; i++ {
|
||||||
|
c = a + b
|
||||||
|
a, b = b, c
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -228,7 +228,23 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func minCostClimbingStairs(cost []int) int {
|
||||||
|
dp := make([]int, len(cost))
|
||||||
|
dp[0], dp[1] = cost[0], cost[1]
|
||||||
|
for i := 2; i < len(cost); i++ {
|
||||||
|
dp[i] = min(dp[i-1], dp[i-2]) + cost[i]
|
||||||
|
}
|
||||||
|
return min(dp[len(cost)-1], dp[len(cost)-2])
|
||||||
|
}
|
||||||
|
|
||||||
|
func min(a, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user