Question description
A frog can jump up one step at a time, or it can jump up two steps... It can also jump up n steps. Find out how many ways the frog can jump up an n-level staircase.
Ideas
Number of steps | jumping method |
---|---|
1 | 1 |
2 | 2 |
3 | 4 |
4 | 8 |
5 | 16 |
6 | 32 |
… | … |
Simple and clear, different from the jumping method that can only choose 1 or 2 steps (this method is Fibonacci), the answer can be obtained by recursion.
code
public class Solution {
public int JumpFloorII(int target) {
if(target==0) {
return 0;
}
if(target==1)
return 1;
return 2*JumpFloorII(target-1);
}
}