A backward solution for c++ with 0ms


  • 0
    K
    class Solution {
    public:
        int climbStairs(int n) {
            if (n <= 2) return n;
            vector<int> vec(n + 1, 0);
            vec[n] = 1;
            for (int i = n; i > 2; --i) {
                vec[i - 1] += vec[i];
                vec[i - 2] += vec[i];
            }
            return vec[1] + vec[2] * 2;
        }
    };
    

    A backward solution for c++ with 0ms.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.