题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
对于第n个台阶来说,只能从n-1或者n-2的台阶跳上来,所以
F(n)
= F(n-1) + F(n-2)
斐波拉契数序列,初始条件
n=1:只能一种方法
n=2:两种
class Solution {
public:
int jumpFloor(int number) {
if(number == 0)
{
return 0;
}
if(number == 1)
{
return 1;
}
if(number == 2)
{
return 2;
}
int n1 = 1;
int n2 = 2;
int total = 0;
for(int i = 2;i < number;i++)
{
total = n1 + n2;
n1 = n2;
n2 = total;
}
return total;
}
};