Combination Sum II

来源:互联网 发布:软件书生商务网 编辑:程序博客网 时间:2024/06/05 16:18
class Solution(object):
    def combinationSum2(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        candidates.sort()
        res=[]
        line=[]
        self.helper(candidates,target,res,line)
        return res
        
    def helper(self,candidates,target,res,line):
        if target==0:
            res.append([x for x in line])
        tmp=0
        for i,x in enumerate(candidates):
            if x==tmp:
                continue
            else:
                tmp=candidates[i]
            if x<=target:
                line.append(x)
                self.helper(candidates[i+1:],target-x,res,line)

                line.pop()

加一个临时空间,存放前一个数,如果数相等的话直接跳出进行下一次循环

原创粉丝点击