链接
牛客:跳台阶
LeetCode:剑指 Offer 10- II. 青蛙跳台阶问题
思路
和斐波那契那题类似。
代码
牛客:
public class Solution {
public int JumpFloor(int num) {
if (num == 0 || num == 1 || num == 2)
return num;
int first = 1;
int second = 2;
int res = 0;
for (int i = 3; i <= num; i++) {
res = first + second;
first = second;
second = res;
}
return res;
}
}
LeetCode:
class Solution {
public int numWays(int n) {
if (n == 0 || n == 1) {
return 1;
}
int first = 1, second = 1;
int result = 0;
for (int i = 2; i <= n; i++) {
result = (first + second) % 1000000007;
first = second;
second = result;
}
return result;
}
}
牛客和 LeetCode 的区别在于 n 的取值范围还有是否取模