码迷,mamicode.com
首页 > 其他好文 > 详细

Hints

时间:2014-07-16 18:14:59      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   2014   for   io   

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:

 bubuko.com,布布扣                      

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:

 bubuko.com,布布扣

 

from Thinking in Python

 

 

Hints,布布扣,bubuko.com

Hints

标签:style   blog   http   2014   for   io   

原文地址:http://www.cnblogs.com/ryansunyu/p/3845415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!