Climbing Stairs
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.
解题思路:
这道题大概思路是这样的。第n格梯子,可以是从第n-1格走一格上来,也可以是从第n-2格走两格上来,所以第n格的可能性等于第n-1格的可能性+第n-2格的可能性。
所以这道题是在求斐波那契数列的第n项....
public class Solution {
public int climbStairs(int n) {
int x1 = 0;
int x2 = 1;
for(int i = 0;i<n;i++)
{
int r = x2+x1;
x1 = x2;
x2 = r;
}
return x2;
}
}