39. Combination Sum 40. Combination Sum II

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.


39. Combination Sum 40. Combination Sum II_第1张图片
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. Combination Sum II)