思路:
每一阶台阶只能从前面一阶或者前两阶跳过来。
代码:
class Solution {
public:
vector<int> f;
const int N = 1e9+7;
int numWays(int n) {
if(n < 2) return 1;
f = vector<int>(n + 1);
f[0] = f[1] = 1;
for(int i = 2; i <= n; i++){
f[i] = (f[i - 1] + f[i - 2]) % N;
}
return f[n];
}
};
评论区