美文网首页
39. Combination Sum 40. Combin

39. Combination Sum 40. Combin

作者: GoDeep | 来源:发表于2018-05-11 21:06 被阅读0次

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

    The same repeated number may be chosen from candidates unlimited number of times.

    Note:

    All numbers (including target) will be positive integers.
    The solution set must not contain duplicate combinations.


    image.png

    如果DFS函数内部,考虑当前i位置拿不拿,39这样写是有重复的结果;
    40这样写是会有漏掉的情况
    先看错误的答案

    class Solution(object):
        def combinationSum(self, a, target):
            """
            :type candidates: List[int]
            :type target: int
            :rtype: List[List[int]]
            """
            res, path = [], []
            a.sort()
            
            # 考虑当前i位置拿不拿,这样写是有重复的结果
            def dfs(i, s):
                if s==target: res.append(list(path))
                if s>=target or i==len(a): return
                
                path.append(a[i])
                dfs(i, s+a[i])
                path.pop()
                
                path.append(a[i])
                dfs(i+1, s+a[i])
                path.pop()
                
                path.append(a[i])
                dfs(i+1, s)
                path.pop()
                
            dfs(0, 0)
            return res
    
    class Solution(object):
        def combinationSum2(self, a, target):
            """
            :type candidates: List[int]
            :type target: int
            :rtype: List[List[int]]
            """
            res, path = [], []
            a.sort()
            
            def dfs(i, s):
                if s==target: res.append(list(path))
                if s>=target or i==len(a): return
                
                # 会漏掉解
                if path and a[i]!=path[-1]:
                    path.append(a[i])
                    dfs(i+1, s+a[i])
                    path.pop()
                
                dfs(i+1, s)
                
            dfs(0, 0)
            return res
    

    在一般的DFS里面可能这样写OK,但是这里需要维护一个start变量,表示以后都从start开始选,然后每次DFS循环就是从start到end选择一个就好了
    39,40不同的就是40因为不能重复,选择完了就跳到start+1,而39还可以继续留在start

    class Solution(object):
        def combinationSum(self, a, target):
            """
            :type candidates: List[int]
            :type target: int
            :rtype: List[List[int]]
            """
            res, path = [], []
            a.sort()
            # 通过start递增来保证不会有重复
            def dfs(start, s):
                if s==target: res.append(list(path))
                if s>=target or start==len(a): return
                for i in range(start, len(a)):
                    path.append(a[i])
                    dfs(i, s+a[i])
                    path.pop()
                
            dfs(0, 0)
            return res
    
    class Solution(object):
        def combinationSum2(self, a, target):
            """
            :type candidates: List[int]
            :type target: int
            :rtype: List[List[int]]
            """
            res, path = [], []
            a.sort()
            def dfs(start, s):
                if s==target: res.append(list(path))
                if s>=target or start==len(a): return
                for i in range(start, len(a)):
                    if i!=start and a[i]==a[i-1]: continue
                    path.append(a[i])
                    dfs(i+1, s+a[i])
                    path.pop()
                
            dfs(0, 0)
            return res
    

    相关文章

      网友评论

          本文标题:39. Combination Sum 40. Combin

          本文链接:https://www.haomeiwen.com/subject/ytyqdftx.html