题解 | #跳台阶#
跳台阶
http://www.nowcoder.com/practice/8c82a5b80378478f9484d87d1c5f12a4
递归:
class Solution1 { public int jumpFloor(int target) { if (target==1){ return 1; } else if (target==2){ return 2; }else { return jumpFloor(target-1)+jumpFloor(target-2); } } }
dp
class Solution { //动态规划+滚动数组 public int jumpFloor(int target) { int dp[] =new int[2]; if (target<0){ return -1; } else if (target==1){ return 1; } else if (target==2){ return 2; } else { dp[0]=1; dp[1]=2; int result=0; for (int i = 2; i <=target-1; i++) { result=dp[0]+dp[1]; dp[0]=dp[1]; dp[1]=result; } return dp[1]; } } }