leetcode40. 组合总和 II

2018-10-24  本文已影响10人  冰源
组合总和 II
class Solution:
    def combinationSum2(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        res = []
        def dfs(remain,index,comb):
            if remain==0 and comb not in res:  #difference
                res.append(comb)
                return
            for i in range(index,len(candidates)):
                if remain<candidates[i]:
                    break
                dfs(remain-candidates[i],i+1,comb+[candidates[i]]) #difference in 'i+1'
        candidates.sort()
        dfs(target,0,[])
        return res
上一篇 下一篇

猜你喜欢

热点阅读