题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。
答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
示例 1: 输入:n = 2 输出:2
示例 2: 输入:n = 7 输出:21
示例 3: 输入:n = 0 输出:1
题解
动态规划
思路见剑指 Offer 10- I. 斐波那契数列(简单题)
class Solution {
public:
int numWays(int n) {
if(n == 0 || n == 1){
return 1;
}
int firstWays = 1;//从第0级台阶跳上第0级台阶
int secondWays = 1;//从第0级台阶跳上第1级台阶
int totalWays = 0;
while (n >= 2) {
totalWays = (firstWays + secondWays) % 1000000007;
firstWays = secondWays;
secondWays = totalWays;
n--;
}
return totalWays;
}
};
或者for循环
class Solution {
public:
int numWays(int n) {
int a = 1, b = 1, sum;
if(n <= 1 ) return 1;
for(int i = 2; i <= n; i++){
sum = (a + b) % 1000000007;
a = b;
b = sum;
}
return sum;
}
};