【发布时间】:2020-11-05 06:12:11
【问题描述】:
我正在尝试使用递归的方法来解决 Leetcode 上的"Combination Sum" 问题。
组合和问题
- 给定一个不同整数数组
candidates和一个目标整数target,返回所有唯一组合的列表@ 987654325@其中所选数字的总和为target。您可以按任何顺序返回组合。- 相同数字可以从
candidates中选择无限次。如果至少一个所选数字的频率不同,则两个组合是唯一的。 示例Input: candidates = [2,3,5], target = 8 Output: [[2,2,2,2],[2,3,3],[3,5]]
当我使用 "c = c[:-1]" 去除 "c" 的结尾元素时,我无法得到正确的结果。但是,在我将“c = c[:-1]”替换为“c.pop()”之后,结果就正确了。
看完this post,我的理解是
- "list.pop()" 将对原始列表进行更改,而"list[:-1]" 将创建一个新列表。
- “list.pop()”和“list=list[:-1]”会得到相同的结果
但在我的递归方法中,显然“list=list[:-1]”并没有完成工作。我想知道为什么递归函数中的“list.pop”和“list=list[:-1]”之间存在差异。为什么list=list[:-1]会在递归方法中出错?
这是我的代码:
"""
def findCombination(self, nums: List[int], target: int,
index: int, c: List[int],
res: List[List[int]]):
"""
def findCombination(nums, target, index, c, res):
if target <= 0:
if target == 0:
res.append(c.copy())
return
for i in range(index, len(nums)):
if nums[i] > target:
break
c.append(nums[i])
print(f"self.findCombination({nums}, {target - nums[i]}, {i}, {c}, {res})")
findCombination(nums, target - nums[i], i, c, res)
c.pop()
# c = c[:-1]
if __name__ == "__main__":
candidates = [2, 3, 5]
target = 5
c, res = [], []
findCombination(candidates, target, 0, c, res)
print(f"Combinations: {res}")
"""
Using c.pop()
---------------------
self.findCombination([2, 3, 5], 3, 0, [2], [])
self.findCombination([2, 3, 5], 1, 0, [2, 2], [])
self.findCombination([2, 3, 5], 0, 1, [2, 3], [])
self.findCombination([2, 3, 5], 2, 1, [3], [[2, 3]])
self.findCombination([2, 3, 5], 0, 2, [5], [[2, 3]])
Combinations: [[2, 3], [5]]
Using c = c[:-1]
---------------------
self.findCombination([2, 3, 5], 3, 0, [2], [])
self.findCombination([2, 3, 5], 1, 0, [2, 2], [])
self.findCombination([2, 3, 5], 0, 1, [2, 3], [])
self.findCombination([2, 3, 5], 2, 1, [2, 3], [[2, 3]]) # Here, mistask, 2 didn't be popped
self.findCombination([2, 3, 5], 0, 2, [2, 5], [[2, 3]])
Combinations: [[2, 3], [2, 5]]
"""
【问题讨论】: