Share my 0ms C++ solution,O(1) space complexity


  • 2
    V
    class Solution {
    public:
        int climbStairs(int n) {
            int dp1 = 1, dp2 = 2, dpRet = 0;
            if (n <= 1) return dp1;
            if (n == 2) return dp2;
            
            int i = 3;
            while (i++ <= n)
            {
                dpRet = dp1 + dp2;
                dp1 = dp2;
                dp2 = dpRet;
            }
            
            return dpRet;
        }
    };

Log in to reply
 

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