标签:sum class self ret bst 楼梯 python turn air
//到达n阶只可能来自n-1和n-2,所以f(n)=f(n-1)+f(n-2)
//1 2 3 5 8 13
int climbStairs(int n){
if (n == 1)
return 1;
if (n == 2)
return 2;
int sta = 0, sta1 = 1, sta2 = 2;
for (int i = 3; i <= n; i++) {
sta = sta1 + sta2;
sta1 = sta2;
sta2 = sta;
}
return sta;
}
class Solution:
def climbStairs(self, n: int) -> int:
if n == 1:
return 1
if n == 2:
return 2
stat1, stat2 = 1, 2
for i in range(3, n+1):
sum = stat1 + stat2
stat1 = stat2
stat2 = sum
return sum
标签:sum class self ret bst 楼梯 python turn air
原文地址:https://www.cnblogs.com/xuzhaoping/p/11494506.html