Leetcode 40. Combination Sum II
Python 解题思路
class Solution:
def __init__(self):
self.res = []
self.path = []
self.candidates = []
def dfs(self, target, ind):
if target < 0:
return
if target == 0:
self.res.append(self.path.copy())
return
for i in range(ind, len(self.candidates)):
if i > ind and self.candidates[i] == self.candidates[i-1]:
continue
self.path.append(self.candidates[i])
self.dfs(target - self.candidates[i], i+1)
self.path.remove(self.candidates[i])
def combinationSum2(self, candidates, target):
self.candidates = sorted(candidates)
self.dfs(target, 0)
return self.res