一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
//此题思路还是和斐波那契数列类似只是初始值f(0) = 1、f(1) = 2;公式还是f(n) = f(n - 1) + f(n - 2),可以使用递归但是递
//归达不到时间要求
class Solution {
public:
int jumpFloor(int number) {
if(number <= 2)
{
return number;
}
int one_step = 1;
int two_step = 2;
while(number-- > 2)
{
two_step = one_step + two_step; //公式f(n) = f(n - 1) + f(n - 2)的使用
one_step = two_step - one_step;
}
return two_step;
}
};