Skip to content

LeetCode 39. 组合总和 中等

作者:Choi Yang
更新于:5 个月前
字数统计:662 字
阅读时长:3 分钟
阅读量:

题目描述

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

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

说明:

javascript
所有数字(包括 target)都是正整数。
解集不能包含重复的组合。

示例 1:

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

示例 2:

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

提示:

javascript
1 <= candidates.length <= 30
1 <= candidates[i] <= 200
candidate 中的每个元素都是独一无二的。
1 <= target <= 500

来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/combination-sum 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路

这道题是组合题,但是这道题有意思的是当前元素可以重复无限制选取,那么我们可以改一下另外一道组合题的思路,下一层也从 i开始即可,然后本题元素重复,那么我们不需要进行排序然后剪枝了。

javascript
// 当前元素可以无限制选取,下一层也从i开始取
dfs(t.slice(), i, sum + candidates[i]);

参考 xiao_ben_zhu 图解

javascript
var combinationSum = function (candidates, target) {
  let res = [];
  let dfs = (t, start, sum) => {
    if (sum >= target) {
      // 防止爆掉
      if (sum === target) {
        res.push(t);
      }
      return;
    }
    for (let i = start; i < candidates.length; i++) {
      t.push(candidates[i]);
      // 当前元素可以无限制选取,下一层也从i开始取
      dfs(t.slice(), i, sum + candidates[i]);
      t.pop();
    }
  };
  dfs([], 0, 0);
  return res;
};
cpp
class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> res;
        vector<int> t;
        dfs(candidates, target, res, t, 0, 0);
        return res;
    }
    void dfs(vector<int>& candidates, int target, vector<vector<int>>& res, vector<int>& t, int sum, int start) {
        if (sum >= target) {
            if (sum == target) {
                res.push_back(t);
            }
            return;
        }
        for (int i = start; i < candidates.size(); i++) {
            t.push_back(candidates[i]);
            dfs(candidates, target, res, t, sum + candidates[i], i);
            t.pop_back();
        }
    }
};
java
class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> t = new ArrayList<>();
        dfs(candidates, target, res, t, 0, 0);
        return res;
    }
    public void dfs(int[] candidates, int target, List<List<Integer>> res, List<Integer> t, int sum, int start) {
        if (sum >= target) {
            if (sum == target) {
                res.add(new ArrayList<>(t));
            }
            return;
        }
        for (int i = start; i < candidates.length; i++) {
            t.add(candidates[i]);
            dfs(candidates, target, res, t, sum + candidates[i], i);
            t.remove(t.size() - 1);
        }
    }
}
python
class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        res = []
        def dfs(candidates, target, res, t, sum, start):
            if sum >= target:
                if sum == target:
                    res.append(t[:])
                return
            for i in range(start, len(candidates)):
                t.append(candidates[i])
                dfs(candidates, target, res, t, sum + candidates[i], i)
                t.pop()
        dfs(candidates, target, res, [], 0, 0)
        return res
javascript
学如逆水行舟,不进则退

Contributors

Choi Yang