java:
class Solution {
public int climbStairs(int n) {
int p=0,q=0,r=1;
for(int i=0;i<n;i++){
p=q;
q=r;
r=p+q;
}
return r;
}
}
大佬们直接用公式也是我没想到 好强
class Solution {
public int climbStairs(int n) {
double sqrt_5=Math.sqrt(5);
double fib_n=Math.pow((1+sqrt_5)/2,1+n)-Math.pow((1-sqrt_5)/2,1+n);
return (int)(fib_n/sqrt_5);
}
}
python3:
class Solution:
def climbStairs(self, n: int) -> int:
p, q, r = 0, 0, 1
for i in range(n):
p = q
q = r
r = p + q
return r

551




被折叠的 条评论
为什么被折叠?



