본문 바로가기
코드

Fibonacci Number

by ehei 2019. 10. 24.
제목이 곧 내용... https://leetcode.com/problems/fibonacci-number/
class Solution:
    def fib(self, N: int, cache = None) -> int:
        if N == 0:
            return 0
        elif N == 1:
            return 1
        else:
            if cache is None:
                cache = {}
            
            if N in cache:
                return cache[ N ]
            else:
                result = self.fib( N - 1, cache ) + self.fib( N - 2, cache )
                cache[ N ] = result
                
                return result

'코드' 카테고리의 다른 글

Maximum Depth of Binary Tree  (0) 2019.10.24
Climbing Stairs  (0) 2019.10.24
Reverse Linked List  (0) 2019.10.24
Swap Nodes in Pairs  (0) 2019.10.24
Reverse String  (0) 2019.10.24