Sharing my 0ms C++ solution


  • 2
    T
    class Solution {
    public:
        int climbStairs(int n) {
            int DP[n+1];
            DP[1] = 1;
            DP[2] = 2;
            int i;
            for(i=3; i<=n; i++)
                DP[i] = DP[i-2] + DP[i-1];
            return DP[n];
        }
    };

  • 0
    L

    class Solution {
    public:
    int climbStairs(int n) {
    if (n<=1)
    return n;
    int *memo = new int[n+1];
    memo[0] = 1;
    memo[1] = 1;
    for(int i = 2; i<=n;i++){
    memo[i] = memo[i-1]+ memo[i-2];
    }
    return memo[n-1]+memo[n-2];
    }
    };


Log in to reply
 

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