题目
思路
dfs,注意去重即可。
代码
class Solution:
def __init__(self):
self.res_list = []
def dfs(self, candidates, target, num, startIndex):
if target < 0: return
if target == 0:
self.res_list.append(num[:])
return
for i in range(startIndex, len(candidates)):
if i != startIndex and candidates[i] == candidates[i - 1]: continue
num.append(candidates[i])
self.dfs(candidates, target - candidates[i], num[:], i + 1)
num.pop(-1)
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
candidates.sort()
self.dfs(candidates, target, [], 0)
return self.res_list