因为小青蛙一次只能跳1/2个台阶,因此如果要跳到第n个台阶,只能从n-1或者n-2台阶跳上去。典型的斐波那契数列,只不过第0项也为1,只有这点区别。
class Solution {
public:
int numWays(int n) {
if(n < 2){
return 1;
}
int fib_N = 0,fib_O = 1,fib_T = 1;
for(int i = 2;i <= n; i++){
fib_N = (fib_O + fib_T) % 1000000007;
fib_O = fib_T;
fib_T = fib_N;
}
return fib_N;
}
};