二刷
因为只与前两个有关 所以 不需要一个dp 只需要两个变量
public class Solution { public int jumpFloor(int target) { int[] dp = new int[target+1]; for(int i=0;i<=target;i++){ if(i<=2) dp[i] = i; else dp[i] = dp[i-1] +dp[i-2]; } return dp[target]; } }
动态规划
public class Solution { public int jumpFloor(int target) { if(target <=2){ return target; } int pre2 = 1; int pre1 = 2; for(int i=3; i<=target; i++){ int cur = pre2 + pre1; pre2 = pre1; pre1 = cur; } return pre1; } }