一、题目内容

image.png

二、题解

解法1:

思路

动态规划
f(0)=1
f(1)=1
f(2)=2
dp[i] = dp[i-1]+dp[i-2]

代码

  1. public class Solution {
  2. public int jumpFloor(int target) {
  3. if (target == 0 || target == 1) {
  4. return 1;
  5. }
  6. if (target == 2) {
  7. return 2;
  8. }
  9. return jumpFloor(target - 1) + jumpFloor(target-2);
  10. }
  11. }

解法2:

思路

双指针
a=1,b=1,c=0
for(int i = 2;i<=target;i++)
c = a + b
a = b
b = c

代码

  1. public class Solution {
  2. public int jumpFloor(int target) {
  3. if(target<=1){
  4. return 1;
  5. }
  6. int a = 1,b = 1,c = 0;
  7. for(int i = 2;i<=target;i++){
  8. c = a+b;
  9. a = b;
  10. b = c;
  11. }
  12. return c;
  13. }
  14. }