509. 斐波那契數

 

 

 

 

 

 

非遞歸實現。app

 1 class Solution(object):
 2     def fib(self, N):
 3         """
 4         :type N: int
 5         :rtype: int
 6         """
 7         fibs = []
 8         fibs.append(0)
 9         fibs.append(1)
10         if N <= 1:
11             return fibs[N]
12         for i in range(2, N + 1):
13             fibs.append(fibs[i - 2] + fibs[i - 1])
14         return fibs[-1]
15 
16 
17 if __name__ == '__main__':
18     solution = Solution()
19     print(solution.fib(0))
相關文章
相關標籤/搜索