【发布时间】:2012-04-26 18:36:23
【问题描述】:
基本上我在 Python 中有这个巨大的功能(我已经简化为最基本的功能)
def rec(a,b):
if stoppingCondition==True: return 1
key=(a,b)
if key in memo: return memo[key]
if b==side condition:
memo[key]=rec(a+1,b) #RECURSIVE CALL
return memo[key]
total=0
for d in D:
if condition1==True:
b=some process 1
total+=rec(a+1,b) #RECURSIVE CALL
elif condition2==True:
for x,y in d:
if (some break condition==True): break
else: #only gets called if break didnt happen
b=some process 2
total+=rec(a+1,b) #RECURSIVE CALL
memo[key]=total
return memo[key]
而且我花了很长时间让它迭代,因为它爆炸了更深的递归级别。我已经阅读了有关转换为循环和堆栈之类的其他线程,但我无法让它工作。
【问题讨论】:
-
我正在尝试一种堆栈方法
标签: recursion