递推公式:
和斐波那契数列是一致的
1.暴力办法,时间复杂度O(2^n)
public class Solution {
public int JumpFloor(int n) {
if (n == 1) return 1;
if (n == 2) return 2;
return JumpFloor(n - 1) + JumpFloor(n - 2);
}
}
2.动态规划
class Solution {
static final int MODULO = 1000000007;
public int numWays(int n) {
if (n <= 1) {
return 1;
}
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = (dp[i - 1] + dp[i - 2]) % MODULO;
}
return dp[n];
}
}
标签:return,JumpFloor,int,class,青蛙,Solution,台阶,public
From: https://www.cnblogs.com/chenyi502/p/17370345.html