如何在递归 python 函数中冒泡一个数组?
python 230
原文标题 :How can i bubble up an array in a recursive python function?
我正在学习动态编程,我正在观看的视频显示了 JS 中的问题的解决方案,如下所示:
我想弄清楚如何用 Python 编写这个解决方案,我已经制作了这样的递归函数:
def howSum(nums: List[int], target: int):
if target == 0:
return []
elif target < 0:
return None
for num in nums:
rem = target - num
res = howSum(nums, rem)
if res:
res.append(num)
return res
return None
print(howSum([2,3,5], 8))
但是我的函数返回None
instad返回数组[2,2,2,2]
。
将此函数从 JS 转换为 Python 时我做错了什么吗?