我想比较python中两个递归函数中变量之间的差异。第一个是:
def helper(nums, target, count):
print(count)
if target < 0:
return
elif target == 0:
count += 1
else:
for i in range(0, len(nums)):
helper(nums, target-nums[i], count)
count = 0
helper([1, 2, 3], 4, count)
计数变量始终为 0。
第二个是:
def helper(nums, target, path, res):
print(res)
if target < 0:
return
elif target == 0:
res.append(path)
else:
for i in range(0, len(nums)):
helper(nums, target-nums[i], path + [nums[i]], res)
path = []
res = []
helper([1, 2, 3], 4, path, res)
资源会不断变化。
谁能告诉我为什么 count 变量总是等于 0?
python中递归函数中的整数变量和列表变量有区别吗?
眼眸繁星
相关分类