0ms C++ solution,is just like fibonacci


  • 2
    consider the last time climb 1 step or 2 steps,so the total ways is the sum of the ways of former (n-1)and(n-2)steps.
    
     int climbStairs(int n) {
        if(n<4)return n;
        int a=2,b=3,c=5;
        n -= 4;
        while(n--){
            a=c;
            c=b+c;
            b=a;
        }
        return c;
    }

Log in to reply
 

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