dfs --path sum 问题 本质上就是组合问题(有去重)

135. 数字组合

中文

English

给定一个候选数字的集合 candidates 和一个目标值 target. 找到 candidates 中所有的和为 target 的组合.

在同一个组合中, candidates 中的某个数字不限次数地出现.

样例

样例 1:

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

样例 2:

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

注意事项

  1. 所有数值 (包括 target ) 都是正整数.
  2. 返回的每一个组合内的数字必须是非降序的.
  3. 返回的所有组合之间可以是任意顺序.
  4. 解集不能包含重复的组合.
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. 数字组合 II

中文

English

给定一个数组 num 和一个整数 target. 找到 num 中所有的数字之和为 target 的组合.

样例

样例 1:

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

样例 2:

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

注意事项

  1. 在同一个组合中, num 中的每一个数字仅能被使用一次.
  2. 所有数值 (包括 target ) 都是正整数.
  3. 返回的每一个组合内的数字必须是非降序的.
  4. 返回的所有组合之间可以是任意顺序.
  5. 解集不能包含重复的组合.
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()

原文地址:https://www.cnblogs.com/bonelee/p/11668915.html

时间: 2024-08-30 18:03:26

dfs --path sum 问题 本质上就是组合问题(有去重)的相关文章

Leetcode dfs Path Sum

Path Sum Total Accepted: 20393 Total Submissions: 66674My Submissions Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below bi

[email&#160;protected] [124] Binary Tree Maximum Path Sum (DFS)

https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connection

[LeetCode] 437. Path Sum III_ Easy tag: DFS

You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to

113. Path Sum II (Tree; DFS)

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] struct TreeNode { int val

124. Binary Tree Maximum Path Sum (Tree; DFS)

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1      / \     2   3 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

LeetCode:Path Sum - 树的根节点到叶节点的数字之和

1.题目名称 Path Sum(树的根节点到叶节点的数字之和) 2.题目地址 https://leetcode.com/problems/path-sum/ 3.题目内容 英文:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. 中文:给定一颗二叉树,如

124. Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl

LeetCode: Path Sum II [113]

[题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] [题意] 判断二叉树中是否存在一条从根到