You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
分析:斐波那契数列变形
class Solution {
public:
int climbStairs(int n) {
if(n < 3)return n;
int n1 = 1;
int n2 = 2;
int n3;
for(int i = 0; i < n-2; i++)
{
n3 = n2 + n1;
n1 = n2;
n2 = n3;
}
return n3;
}
};