def combinationSum2(candidates, target):
res = []
candidates.sort()
def dfs(target, index, path):
if target < 0:
return # backtracking
if target == 0:
res.append(path)
return # backtracking
for i in range(index, len(candidates)):
if candidates[i] == candidates[i-1]:
continue
dfs(target-candidates[i], i+1, path+[candidates[i]])
dfs(target, 0, [])
return res
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter