dfs --path sum on the nature of the problem is a combination of problems (there are de-emphasis)

135. The number combination

Chinese
English

Given a set of candidate digital candidatesand a target targetFound candidatesall and for the targetcombinations.

In a combination of same, candidatesin an unlimited number appears.

Sample

Example 1:

输入: candidates = [2, 3, 6, 7], target = 7
输出: [[7], [2, 2, 3]]

Sample 2:

输入: candidates = [1], target = 3
输出: [[1, 1, 1]]

Precautions

  1. All values (including target) are positive integers.
  2. The number in each combination of a return must be non-descending.
  3. Return among all combinations may be in any order.
  4. Solution Set can not contain duplicate combinations thereof.
class Solution:
    """
    @param candidates: A list of integers
    @param target: An integer
    @return: A list of lists of integers
    """
    def combinationSum(self, candidates, target):
        # write your code here
        result = []
        self.dfs(sorted(list(set(candidates))), target, result, path=[], start_index=0)
        return result
    
    def dfs(self, nums, target, result, path, start_index):
        if target == 0 and path:
            result.append(list(path))
        
        if target < 0:
            return
        
        for i in range(start_index, len(nums)):
            path.append(nums[i])
            self.dfs(nums, target-nums[i], result, path, i)
            path.pop()

 

153. The combination of numbers II

Chinese
English

Given an array numand an integer target. Find numall the numbers for the sum of the targetportfolio.

Sample

Example 1:

输入: num = [7,1,2,5,1,6,10], target = 8
输出: [[1,1,6],[1,2,5],[1,7],[2,6]]

Sample 2:

输入: num = [1,1,1], target = 2
输出: [[1,1]]
解释: 解集不能包含重复的组合

Precautions

  1. In a combination of the same, numeach of a number can only be used once.
  2. All values (including target) are positive integers.
  3. The number in each combination of a return must be non-descending.
  4. Return among all combinations may be in any order.
  5. Solution Set can not contain duplicate combinations thereof.
class Solution:
    """
    @param num: Given the candidate numbers
    @param target: Given the target number
    @return: All the combinations that sum to target
    """
    def combinationSum2(self, nums, target):
        # write your code here
        result = []
        self.dfs(sorted(nums), target, result, path=[], start_index=0)
        return result
     
    def dfs(self, nums, target, result, path, start_index):
        if target == 0 and path:
            result.append(list(path))
         
        if target < 0:
            return
         
        for i in range(start_index, len(nums)):
            if i > 0 and nums[i] == nums[i-1] and i > start_index:
                continue
            path.append(nums[i])
            self.dfs(nums, target-nums[i], result, path, i+1)
            path.pop()

 

Guess you like

Origin www.cnblogs.com/bonelee/p/11668915.html