Question:
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?
Answer:
class Solution {public:
int climbStairs(int n) {
if(n==0) return 0;
int fn_2 = 1, fn_1=2, fn;
if(n==1) return fn_2;
if(n==2) return fn_1;
//if(n>=3)
for(int i=3;i<=n;++i){
fn = fn_1 + fn_2;
fn_2 = fn_1;
fn_1 = fn;
}
return fn_1;
}
};
No comments:
Post a Comment