leetcode70Climbing 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?
一开始,这个题我有点看不懂,,然后去百度了下别人的,,,算是了解了,,当n为1的时候,1种方法,2的时候,2种,3的时候3种,4的时候5种,5的时候8种,当n大于3之后,就是我们所熟悉的斐波那契数列了,,,然后这个题目就很简单了。。。。话说这题要不是看看别人的提示,,我还真不会做。。。
class Solution {
public:
int climbStairs(int n) {
if (n == 1 || n == 0)
return 1;
if (n == 2)
return 2;
if (n == 3)
return 3;
int pre = 2, cur = 3, temp;
for (int i = 4; i <= n; i++)
{
temp = cur;
cur += pre;
pre = temp;
}
return cur;
}
};