o(n) time, o(1) space C++


  • 0
    R
    int climbStairs(int n) {
        if(n == 0||n==1)
            return n;
       int b = 1;
       int a = 2;    
       for(int i=3;i<=n;i++){
           a = a+b;
           b = a-b; 
       }
       return a; 
    }

Log in to reply
 

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