Hints

If you played with the Fibonacci function, you might have noticed that the bigger the argument you provide, the longer the function takes to run. Furthermore, the run time increases very quickly.

To understand why, consider this call graph for Fibonacci with n = 4:

                       

A call graph shows a set function frames, with lines connecting each frame to the frames of the functions it calls. At the top of the graph, Fibonacci with n=4 calls Fibonacci with n=3 and n=2. In turn, Fibonacci with n=3 calls Fibonacci with n=2 and n=1. And so on. Count how many times Fibonacci(0) and Fibonacci(1) are called. This is an inefficient solution to the problem, and it gets worse as the argument gets bigger. One solution is to keep track of values that have already been computed by sorting them in a dictionary. A previously computed value that is stored for later use is called a hint. Here is an implementation of Fibonacci using hints:

 

 

from Thinking in Python

 

 

转载于:https://www.cnblogs.com/ryansunyu/p/3845415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值