标签:print else return 效率 fibonacci python def efficient ==
原代码:
def fib(n):
if n == 1:
return 1
elif n == 2:
return 2
else:
return fib(n-1)+fib(n-2)
改进后:
def fib_efficient(n,d):
if n in d:
return d[n]
else:
ans = fib_efficient(n-1,d) + fib_efficient(n-2,d)
d[n] = ans
return ans
d = {1:1, 2:2}
print(fib_efficient(6,d))
标签:print else return 效率 fibonacci python def efficient ==
原文地址:http://www.cnblogs.com/Bella2017/p/7989565.html