My python iteration process, 32ms,beats 97%


  • 0
    F
    class Solution(object):
    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        """
        if n==1:
           return 1
        elif n==2:
           return 2
          
        return self.climbStairs(n-1)+self.climbStairs(n-2)
        
        and the leetcode saids this exceed the time limit 
        but I found that this is a Fibonacci numbers
        so I write this iteration process to produce Fibonacci number,
        and NOTICE this is not recurssion,it's is iteration
        """
        
        def iterate_fibonacci(a,b,i):
            if i>n:
                return b
            else:
                return iterate_fibonacci(b,a+b,i+1)
                
        return iterate_fibonacci(0,1,1)

Log in to reply
 

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