我很好奇为什么第一个函数确实起作用(OOP),但是第二个触发器却出错了。有人可以解释吗?
class Solution(object):
def __init__(self):
self.solved = {0:0,1:1,2:1}
def fibonacci(self, n):
"""
:type n: int
:rtype: int
"""
if n in self.solved.keys():
return self.solved[n]
else:
value = self.fibonacci(n-1) + self.fibonacci(n-2)
self.solved[n] = value
return value
t = Solution()
t.fibonacci(7)
>>> 13
但这行不通
found = {0:0,1:1,2:1}
def fibonacci(n):
if n in found.keys():
return found[n]
else:
value = fibonacci(n-1) + fibonacci(n-2)
found[n] = value
return value
fibonacci(7)
>>>TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'