LeetCode 509: Fibonacci Number
class Solution:
def fib(self, N: int) -> int:
if N == 0:
return 0
if N == 1:
return 1
memo = [None] * (N+1)
return self.recurse(N, memo)
def recurse(self, N: int, memo: List) -> int:
if N == 0:
return 0
elif N == 1:
return 1
elif memo[N] != None:
return memo[N]
memo[N] = self.recurse(N-1, N-2)
return memo[N]
I am getting an "int object not subscriptable" error on the line "elif memo[n] != None:". However, memo is a list not an int. I can't figure out why I am getting this error. Maybe it has to do with the fact that I initialized my List with all None elements? Any help would be appreciated. Thank you!
memo[N] = self.recurse(N-1, N-2)you pass a number as the second argument torecurse, somemois a number, not a list.