2ms c++ solution / O(1) space


  • 0
    P
    int climbStairs(int n) {
        if (n == 1) return 1;
        int temp1 = 1;
        int temp2 = 2;
        for (int i = 3; i <= n; ++i) {
            int temp = temp2;
            temp2 = temp1 + temp2;
            temp1 = temp;
        }
        return temp2;
    }

Log in to reply
 

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