class Solution(object):
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
candidates.sort()
res = [] # save the result
path = [] # save each unique combination
self.dfs(0, target, path, res, candidates)
return res
def dfs(self, start_index, target, path, res, candidates):
if target == 0:
res.append(path)
return
if target < 0:
path.pop()
return
for i in range(start_index, len(candidates)):
if i > start_index and candidates[i] == candidates[i-1]:
continue
path += [candidates[i]]
self.dfs(i+1, target-candidates[i], path, res, candidates)
if path:
path.pop()
The above code for LeetCode problem Combination Sum II, given the input [10,1,2,7,6,1,5]
and 8
, the output should be [[1,1,6],[1,2,5],[1,7],[2,6]]
. However, the output of this piece of code run to be [[],[],[],[]]
, I'm very confusing and wondering why :(