0070.爬楼梯
方法一:动态规划
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
func climbStairs(n int) int {
if n < 3 {
return n
}
first, second, three := 1, 2, 0
for i := 3; i <= n; i++ {
three = first + second
first, second = second, three
}
return three
}
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
func climbStairs(n int) int {
if n < 3 {
return n
}
first, second, three := 1, 2, 0
for i := 3; i <= n; i++ {
three = first + second
first, second = second, three
}
return three
}