温馨提示×

python怎么强制结束递归函数

九三
1558
2021-02-18 19:58:06
栏目: 编程语言

python怎么强制结束递归函数

在python中强制结束递归函数的方法以下两种

1.使用return语句结束

def count(dict, key, depth):

if key is not None:

if key == 42:

return depth

return count(map, map[key][0], depth+1)

return count(map, map[key][1], depth+1)

2.通过在递归函数外定义一个isGo全局变量结束

var isGo:Boolean=true

functionarr_all(pre:Array,nex:Array) {

if(isGo==false){return}

var j:uint=nex.length;

if (j==1) {

var t=[];

for (var i:uint=0;it.push(pre[i]);

}

t.push(nex[0]);

isGo =false;

returninfo.appendText("\n"+pc(t));

}

for (var k:uint=0; kvar p:Array=pre.slice();

var s:Array=nex.slice();

p.push(s.splice(k,1));

arr_all(p,s);

}

}

0