您的位置:首页 > 其它

leetcode刷题,总结,记录,备忘70

2015-06-26 22:30 351 查看
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;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: