题目描述
其实可以看作斐波那契数列问题或者跳台阶问题。
C++代码
#include <iostream>
using namespace std;
class Solution {
public:
// 思路: 找规律,发现解法类似于斐波那契数列:1,2,3,5,8,13...
int jumpFloor(int number) {
if (number <= 0)
return -1;
int j_2 = 1;
int j_1 = 2;
int j = 0;
if (number == 1 || number == 2)
return number;
while (number >= 3){
j = j_2 + j_1;
j_2 = j_1;
j_1 = j;
number--;
}
return j;
}
};
int main()
{
Solution s;
int n;
cin >> n;
cout<<s.jumpFloor(n);
return 0;
}