problem
A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage.
The frog jumped seeking a total of n grade level how many jumps.
Algorithm 1
dp问题
- When n = n, there are n hops manner, the first-order, second order ... n order, concluded that:
F (n) = F (n-1) + F (n-2) + ... + F (n - (n-1)) + f (nn)- f (n-1) = f (0) + f (1) + f (2) + f (3) + ... + f ((n-1) -1)
of both can be reduced to l
f(n)=2f(n-1)
public int JumpFloorII(int target) {
if(target==1){
return 1;
}
if(target==2){
return 2;
}
//将大问题划分为小问题进行解决
return JumpFloorII(target-1)*2;
}
Algorithm 2
Each step has to jump and not jump in both cases (except the last step), the last step must jump. Therefore, the common 2 (n-1) in the case where ^
//每一个台阶都有跳与不跳两种情况(除了最后一个台阶)最后一个台阶必须跳,所以共有2^(n-1)中情况
public int JumpFloorII2(int target){
return (int)Math.pow(2,target-1);
}