如果我的算法使用递归或memoization的? [英] Should I use recursion or memoization for an algorithm?

查看:278
本文介绍了如果我的算法使用递归或memoization的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有选择使用递归或记忆化来解决这应该使用有问题?换句话说,如果他们都是可行的解决方案,他们给正确的输出,并且可以pssed在code我使用合理EX $ P $的时候,我会用一个比其他?

If I have a choice to use recursion or memoization to solve a problem which should I use? In other words if they are both viable solutions in that they give the correct output and can be reasonably expressed in the code I'm using, when would I use one over the other?

推荐答案

我挑记忆化,因为它通常可以访问多个堆内存超过堆栈存储器。

I pick memoization because it's usually possible to access more heap memory than stack memory.

也就是说,如果你的算法运行在大量的数据,在大多数语言中你会遇到的堆栈空间递归你在堆上保存的数据空间用完了。

That is, if your algorithm is run on a lot of data, in most languages you'll run out of stack space recursing before you run out of space on the heap saving data.

这篇关于如果我的算法使用递归或memoization的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆