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?
其实这是一个斐波那契数列,如果用递归方式来解决,时间会超出,AC不通过,用非递归方式可以通过。
class Solution {
public:
int climbStairs(int n) {
if(n<3)
return n;
else{
int f1=1,f2=2,fn=0;
for(int i=3;i<=n;++i){
fn=f1+f2;
f1=f2;
f2=fn;
}
return fn;
}
}
};