Very Fast Python Solution 97.63% 32 ms


  • 0
    A
    class Solution(object):
        def climbStairs(self, n):
            if n == 1:
                return 1
            elif n == 2:
                return 2
            else:
                a = 1
                b = 2
                for x in range(2, n):
                    if x%2 == 0:
                        a = a+b
                    else:
                        b = a+b
                if x%2 == 0:
                    return a
                else:
                    return b
    

    I used the trick of Fibonacci numbers to solve the problem. If you do a pattern of tests, you will notice that the answers are Fibonacci numbers, and this just uses it to find the right answer in python. It is very fast and when I submitted it, it was 32 ms and 97.63 %


Log in to reply
 

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