def combinationSum2(candidates, target):
res = []
candidates.sort()
def dfs(target, index, path):
if target < 0:
return # backtracking
if target == 0:
res.append(path)
return
for i in range(index, len(candidates)):
dfs(target-candidates[i], i, 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