题目介绍
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?
解法
思路:
a. n==0, f(n) = 1;
b. 当爬到第n层时,f(n) = f(n-1)+f(n-2);分别代表从第n-1层爬1步+第n-2层爬2步;也就是fibonacci.
int climbStairs(int n) {
int first=1,second=1;
int res;
if(n==0||n==1){
return 1;
}
n = n-1; //计算次数
while(n--){
res = first+second;
first = second;
second = res;
}
return res;
}