Skip to main content

70. Climbing Stairs

分析

经典dp,省去空间存储

题解

func climbStairs(n int) int {
if n <= 2 {
return n
}
a, b := 1, 2

// 1 2 3 5
// dp[i] = dp[i-1] + dp[i-2]
for i := 3; i <= n; i++ {
a, b = b, a + b
}

return b
}