美文网首页
70. Climbing Stairs

70. Climbing Stairs

作者: a_void | 来源:发表于2016-09-12 15:10 被阅读0次

    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?

    class Solution {
    public:
        int climbStairs(int n) {
            /* recursive
            if(n<=0) return 0;
            else if(n==1) return 1;
            else if(n==2) return 2;
            else return  climbStairs(n-1) + climbStairs(n-2);
            */
            if(n <= 0) return 0;
            vector<int> v = vector<int>();
            v.push_back(1);
            v.push_back(2);
            while(v.size() < n) v.push_back(v[v.size()-1] + v[v.size()-2]);
            return v[n-1];
        }
    };
    

    相关文章

      网友评论

          本文标题:70. Climbing Stairs

          本文链接:https://www.haomeiwen.com/subject/rkmpettx.html