《剑指Offer》-10-斐波那契数列/青蛙跳台阶
斐波那契数列
long long mod = 1e9 + 7;
int fib(int n) {
if (n < 2) return n;
int prepre = 0, pre = 1, res;
for (int i = 2; i <= n; i++) {
res = (prepre % mod + pre % mod) % mod;
prepre = pre;
pre = res;
}
return res;
}
感觉效率有点低
青蛙跳台阶
long mod = 1e9 + 7;
int numWays(int n) {
if (n < 2)return 1;
int prepre = 1, pre = 1, res;
for (int i = 2; i <= n; i++) {
res = (prepre % mod + pre % mod) % mod;
prepre = pre;
pre = res;
}
return res;
}
这俩居然真的是一样的