1.描述
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?
Note: Given n will be a positive integer.
2.分析
递推
f(1) = 1;
f(2) = 2;
f(n) = f(n-2) + f(n-1);
3.代码
int climbStairs(int n) {
if (1 == n) return 1;
if (2 == n) return 2;
int f1 = 1;
int f2 = 2;
int f3 = 0;
for (unsigned int i = 3; i <= n; ++i) {
f3 = f1 + f2;
f1 = f2;
f2 = f3;
}
return f3;
}