1.原始写法
def fib(n):
if n<=0:
return 0
elif n == 1:
return 1
else:
Return fib(n-1)+fib(n-2)
2.优化写法:定义一个字典记录已经计算过的
Know = {0:0,1:1}
def fib(n):
if n in Know:
Return Know[n]
else:
Res = fib(n-1)+fib(n-2)
Know[n]=res
return res
参考资料