两种解法,动态规划法运行效率更高
动态规划法
public class Solution {
public int JumpFloor(int target) {
if(target <= 0)
return 0;
if(target == 1)
return 1;
int[] dp = new int[target + 1];
dp[1] = 1;
dp[2] = 2;
//动态规划
for(int i = 3 ; i < target + 1; i++){
dp[i] = dp[i-1] + dp[i-2];
}
return dp[target];
}
}
递归法
public class Solution {
public int JumpFloor(int target) {
if(target <= 0)
return 0;
if(target == 1)
return 1;
if(target == 2)
return 2;
return JumpFloor(target - 1) + JumpFloor(target - 2);
}
}