题目

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例 1:

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。

示例 2:

输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]

示例 3:

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

提示:

1 <= candidates.length <= 30
1 <= candidates[i] <= 200
candidate 中的每个元素都 互不相同
1 <= target <= 500

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

思路

可以基于39. 组合总和 - 图1题的思路稍微修改,本题是要获得和等于39. 组合总和 - 图2的所有组合,元素允许重复使用。

本题需要使用一个变量39. 组合总和 - 图3记录数组和。对于每个数,可以考虑选与不选,选的话,将当前数累加到39. 组合总和 - 图4,另外因为可以使用一个数多次,下一次递归调用时开始考虑的数还是当前数,对应代码中39. 组合总和 - 图5递归调用时39. 组合总和 - 图6不变。如果不选,39. 组合总和 - 图7不变,下一次递归调用时开始考虑的数为下一个数,对应代码中39. 组合总和 - 图8递归调用时39. 组合总和 - 图939. 组合总和 - 图10

代码

代码一

  1. class Solution {
  2. List<List<Integer>> ans;
  3. public List<List<Integer>> combinationSum(int[] candidates, int target) {
  4. ans = new ArrayList<>();
  5. int n = candidates.length;
  6. dfs(0, n, 0, target, candidates, new ArrayDeque<>());
  7. return ans;
  8. }
  9. private void dfs(int begin, int n, int sum, int target, int[] candidates, Deque<Integer> path) {
  10. if (sum == target) {
  11. ans.add(new ArrayList<>(path));
  12. return;
  13. }
  14. if (sum > target || begin == n) {
  15. return;
  16. }
  17. // 当前数选择一次或多次,所以下次开始考虑的下标还是begin
  18. path.offerLast(candidates[begin]);
  19. dfs(begin, n, sum + candidates[begin], target, candidates, path);
  20. path.pollLast();
  21. // 不选择当前数,所以下次开始考虑的下标为begin+1
  22. dfs(begin + 1, n, sum, target, candidates, path);
  23. }
  24. }

代码二 对代码一剪枝

上述代码可以通过对输入数组进行排序可以剪枝,减少不必要的搜索。

对输入数组升序排列,如果sum加当前数已经超过target了,那么就不需要再考虑后面的数了,因为一定也会大于target。

  1. class Solution {
  2. List<List<Integer>> ans;
  3. public List<List<Integer>> combinationSum(int[] candidates, int target) {
  4. // 排序是为了剪枝
  5. Arrays.sort(candidates);
  6. ans = new ArrayList<>();
  7. int n = candidates.length;
  8. dfs(0, n, 0, target, candidates, new ArrayDeque<>());
  9. return ans;
  10. }
  11. private void dfs(int begin, int n, int sum, int target, int[] candidates, Deque<Integer> path) {
  12. if (sum == target) {
  13. ans.add(new ArrayList<>(path));
  14. return;
  15. }
  16. if (sum > target || begin == n) {
  17. return;
  18. }
  19. // 加了这个判断可以达到剪枝效果
  20. if (sum + candidates[begin] <= target) {
  21. // 当前数选择一次或多次,所以下次开始考虑的下标还是begin
  22. path.offerLast(candidates[begin]);
  23. dfs(begin, n, sum + candidates[begin], target, candidates, path);
  24. path.pollLast();
  25. }
  26. // 不选择当前数,所以下次开始考虑的下标为begin+1
  27. dfs(begin + 1, n, sum, target, candidates, path);
  28. }
  29. }

代码三

另一种写法,内部写成了循环形式,也是依次考虑每个数,同样为了避免重复,下次循环开始只考虑当前数后面的数。

  1. class Solution {
  2. public List<List<Integer>> combinationSum(int[] candidates, int target) {
  3. List<List<Integer>> ans = new ArrayList<>();
  4. int n = candidates.length;
  5. dfs(candidates, target, n, 0, 0, new ArrayDeque<>(), ans);
  6. return ans;
  7. }
  8. private void dfs(int[] candidates, int target, int n, int sum, int start, Deque<Integer> path, List<List<Integer>> ans) {
  9. if (sum > target) {
  10. return;
  11. }
  12. if (sum == target) {
  13. ans.add(new ArrayList<>(path));
  14. return;
  15. }
  16. for (int i = start; i < n; i++) {
  17. path.offerLast(candidates[i]);
  18. dfs(candidates, target, n, sum + candidates[i], i, path, ans);
  19. path.pollLast();
  20. }
  21. }
  22. }

代码四 对代码三剪枝

剪枝思路和代码二对代码一的剪枝一样。

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        int n = candidates.length;
        Arrays.sort(candidates);
        dfs(candidates, target, n, 0, 0, new ArrayDeque<>(), ans);
        return ans;
    }

    private void dfs(int[] candidates, int target, int n, int sum, int start, Deque<Integer> path, List<List<Integer>> ans) {
        if (sum > target) {
            return;
        }
        if (sum == target) {
            ans.add(new ArrayList<>(path));
            return;
        }
        for (int i = start; i < n; i++) {
            if (sum + candidates[i] > target) {
                break;
            }
            path.offerLast(candidates[i]);
            dfs(candidates, target, n, sum + candidates[i], i, path, ans);
            path.pollLast();
        }
    }
}