Title description
A frog can jump up to one step at a time or up to two steps. Find the total number of jumping methods for the frog to jump on an n-level step (different order counts as different results).
Example 1
enter
1
return value
1
Example 2
enter
4
return value
5
public class Solution {
/**
* 斐波那契(斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
* 斐波那契数列以如下被以递推的方法定义:
* F(0)=1,F(1)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 2,n ∈ N*)
* 对于第n个台阶来说,只能从n-1或者n-2的台阶跳上来,所以
* F(n) = F(n-1) + F(n-2)
* 斐波拉契数序列,初始条件
* n=1:只能一种方法
* n=2:两种(和斐波那契数列稍微有些不同,0是台阶的起点,0、1、1、2、3、5、8、13、21、34、……
* 当n=2的时候 可以直接从0跳到1 再从1跳到2 或者 从0直接跳到2)
* 递归一下就好了
*/
public int JumpFloor(int target) {
if(target == 0){//原地不动
return 1;
}else if(target == 1){//从原地0跳到1
return 1;
}else{
return JumpFloor(target-1) + JumpFloor(target-2);
}
}
}