40-组合总和II

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。

说明:

  • 所有数字(包括目标数)都是正整数。
  • 解集不能包含重复的组合。

示例

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
示例 2:

输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
[1,2,2],
[5]
]


解法

核心思想:回溯算法

关键点:去重,如果上一个数和当前数字相同,则跳过

class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
begin, end = 0, len(candidates)
if end == 0:
return []
res, path = [], []
candidates.sort()
self.helper(candidates, begin, end, target, path, res)
return res

def helper(self, candidates: List[int], begin: int, end: int, delta: int, path: List[int], res: List[int]):
if delta == 0:
res.append(path[:])
for index in range(begin, end):
delta_new = delta-candidates[index]
if delta_new < 0:
break
# 剪枝
if index != begin and candidates[index-1] == candidates[index]:
continue
path.append(candidates[index])
# 与上题的不同在于每个元素不能重复使用
# 所以下一个区间为[index+1,end)
self.helper(candidates, index+1, end, delta_new, path, res)
path.pop()

来源

LeetCode中该题地址,Click here!

-------------本文结束感谢您的阅读-------------