剑指 Offer 10- II. 青蛙跳台阶问题 - 力扣(LeetCode) (leetcode-cn.com)
运行结果
代码
//提示:跳上n级台阶之前,要么在n-1级台阶,要么在n-2级台阶
class Solution {
public:
int numWays(int n) {
if(n <= 1) return 1;
int M = 1e9 + 7;
int Fn_2 = 1, Fn_1 = 1, Fn = 0;
for(int i = 2; i <= n; ++i){
Fn = (Fn_1 + Fn_2) % M;
Fn_2 = Fn_1;
Fn_1 = Fn;
}
return Fn;
}
};