You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.
an=an−1+an−2
a0=a1=1
public class Solution {
public int climbStairs(int n) {
int a = 1, b = 1;
while(--n>0){
a = (b+=a) - a;
}
return b;
}
}
本文探讨了经典的爬楼梯问题,给出了一个递推公式 an = an-1 + an-2 来解决该问题,并通过 Java 代码实现了计算方法。递推公式基于斐波那契数列,能够求出达到楼梯顶部的不同方式的数量。
765

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



