LeetCode 39. Combination Sum

简介: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的数字可以无限制重复被选取。

v2-35d9ba7b53b60ab3292705c9da923eb0_1440w.jpg

Description



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.


Example 1:


Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
  [7],
  [2,2,3]
]


Example 2:


Input: candidates = [2,3,5], target = 8,
A solution set is:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]


描述



给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。


candidates 中的数字可以无限制重复被选取。


说明:

所有数字(包括 target)都是正整数。

解集不能包含重复的组合。


示例 1:


输入: candidates = [2,3,6,7], target = 7,
所求解集为:
[
  [7],
  [2,2,3]
]


示例 2:


输入: candidates = [2,3,5], target = 8,
所求解集为:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/combination-sum

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


思路

  • 使用深度优先搜索,进行遍历。如果路径的和为 target ,就此路径的值添加到结果数组中。
  • 为了表示路径和为 target,我们将每次的 target 减去当前遍历的元素,那么当 target 为 0 时遍历走过的路径就时题意所求。
  • 首先对给定的数组进行排序,由于元素可以被重复使用,所以下一次遍历的开始位置为当前位置 i,而不是 i +1。
  • 由于数组已经排序,所以如果当前元素大于 target,那么可以直接返回。
  • 结束条件为 targe 为 0。


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2018-11-28 18:20:58
# @Last Modified by:   何睿
# @Last Modified time: 2019-08-18 07:11:56
from typing import List
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        res = []
        candidates.sort()
        self.dfs(candidates, target, 0, [], res)
        return res
    def dfs(self, candidates, target, index, path, res):
        if target == 0:
            res.append(list(path))
        for i in range(index, len(candidates)):
            if candidates[i] > target:
                return
            path.append(candidates[i])
            self.dfs(candidates, target - candidates[i], i, path, res)
            path.pop()

源代码文件在 这里


目录
相关文章
|
6月前
Leetcode 377. Combination Sum IV
赤裸裸的完全背包,属于动态规划的范畴,大家有兴趣可以在网上搜索下其他资料。个人觉得动态规划还是比较难理解的,更难给别人讲清楚,所以这里我直接附上我的代码供大家参考。
25 0
LeetCode 377. Combination Sum IV
给定一个由正整数组成且不存在重复数字的数组,找出和为给定目标正整数的组合的个数。
72 0
LeetCode 377. Combination Sum IV
LeetCode 216. Combination Sum III
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
79 0
LeetCode 216. Combination Sum III
LeetCode 216 Combination Sum III(Backtracking)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51935033 翻译 找出所有的k个数字相加得到数字n的组合,只有1到9的数字可以被使用,并且每个组合间需要是不同的数字集。
714 0
LeetCode - 39. Combination Sum
39. Combination Sum  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个待选集合s和一个数n,让你找出集合s中相加之和为n的所有组合.
806 0
LeetCode - 40. Combination Sum II
40. Combination Sum II  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个待选集合s和一个数n,选出所有相加之和为n的组合.
970 0
|
2月前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2

热门文章

最新文章