Step Jumping Upgrade
A frog can jump up 1 steps at a time, or 2 steps...it can also jump up n steps. Find how many ways the frog can jump up a n-level stair.
Ideas:
Level 1 has 1 method, Level 2 has 2 methods, Level 3 has f(2)+f(1)+1=4 Level 4 has:
f(3)+f(2)+f(1)+1=8
//这个可以用数学来解释:
F(n) = F(n-1)+F(n-2)+...+F(1)+1 F(n-1) = F(n-2)+F(n-3)+...+F(1)+1
Subtracting the two formulas, it is easy to get the F(n)=2F(n-1)
code:
class Solution {
public:
int jumpFloorII(int number) {
int result=2;
if(number==1)
return 1;
if(number==2)
return 2;
return 2*jumpFloorII(number-1);
}
};