Solution in 93 bytes. Can you make shorter? ;)


  • 0
    T
    class Solution{public:int climbStairs(int n){int a=1,b=n&1;for(;n>1;n-=2)a+=b+=a;return a;}};
    

    Only 93 bytes! It can fit in tweet. Can you make shorter solution?


  • 1

    Easily.

    struct Solution{int climbStairs(int n){int a=1,b=1;for(;n--;a=b-a)b+=a;return a;}};
    

    Using struct instead of class saved 6 bytes, using the simpler algorithm saved 4 more.

    I do like your two-steps-at-once way, though.


Log in to reply
 

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