Q: A frog can jump up 1 steps at a time, or 2 steps at a time. Find how many ways the frog can jump up a n-level stair.
A: The essence of this problem is the summation of the Fibonacci sequence, and the recursive formula is f(n)=f(n-1)+f(n-2);
class Solution { public: int jumpFloor(int number) { if(number <= 0) { return 0; } else if(number == 1 || number == 2) { return number; } else { //int re = 0; return jumpFloor(number - 1) + jumpFloor(number - 2); } } };
Q: A frog can jump up 1 steps at a time, and it can jump up 2 steps...it can also jump up n steps. Find how many ways the frog can jump up a n-level stair.
A:f(n) = f(n-1)+f(n-2)+...+f(1)
f(n-1) = f(n-2)+ f(n-3)...+f(1)
Subtract the two equations to get f(n) = 2*f(n-1)class Solution { public: int jumpFloorII(int number) { if(number <= 0) { return 0; } else if(number == 1 || number == 2) { return number; } else { return 2 * jumpFloorII(number - 1); } } };