【题目描述】

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

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

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


​https://leetcode.cn/problems/combination-sum/?favorite=2cktkvj​


【示例】

【LeeCode】39. 组合总和_全排列

【LeeCode】39. 组合总和_重复元素_02

【代码1】

​学习参考​

package com.company;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution2 {
List<List<Integer>> res = new ArrayList<>();
List<Integer> combines = new ArrayList<>();
int sum = 0;

public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
backtrack(0, candidates, target);
System.out.println(res.toString());
return res;
}

private void backtrack(int idx, int[] candidates, int target) {
if(sum == target){
res.add(new ArrayList<>(combines));
return;
}

for (int i = idx; i < candidates.length && sum + candidates[i] <= target; ++i){
sum += candidates[i];
combines.add(candidates[i]);
// 这里以sum为变量, 进行递归, 此时的i是不变的,可重复叠加
// 如果sum + candicates[i] > target 则退出循环, 回到第一次的结果
backtrack(i, candidates, target); // 如果这里的i改成idx 可全排列所有结果
// sum + candicates[i] > target, 移除最后一个数
combines.remove(combines.size() - 1);
sum -= candidates[i];
}
}
}
public class Test {
public static void main(String[] args) {
int[] candidates = {2,3,6,7};
int target = 7;
new Solution2().combinationSum(candidates, target);
}
}


【代码2】

回溯 + List排序 + Set去重 学习参考

package com.company;
import java.util.*;
import java.util.stream.Collectors;

class Solution2 {
private List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
dfs(candidates, 0, target, new Stack<>());
return res;
}

private void dfs(int[] candidates,int start, int target, Stack<Integer> stack) {
if (target == 0){
res.add(new ArrayList<>(stack));
}else if (target >= 2){
for (int i = start; i < candidates.length; i++) {
int num = candidates[i];
stack.push(num);
dfs(candidates, i, target - num, stack);
stack.pop();
}
}
}

}
public class Test {
public static void main(String[] args) {
int[] candidates = {2,3,6,7};
int target = 7;
System.out.println(new Solution2().combinationSum(candidates, target).toString());
}
}


【代码3】

​学习参考​

import java.util.*;

/**
* https://leetcode.cn/problems/combination-sum/solutions/406516/zu-he-zong-he-by-leetcode-solution/
*/
public class Solution {
private static Set<List<Integer>> res = new HashSet<>();
public static void main(String[] args) {
int[] arr = {2, 3, 6, 7};
int target = 7;
combinationSum(arr, target);
System.out.println(res.toString());
}

public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<>();
dfs(candidates, target, new Stack<>());
return list;
}

private static void dfs(int[] candidates, int target, Stack<Integer> stack) {
if (target == 0){
List<Integer> list = new ArrayList<>(stack);
Collections.sort(list);
res.add(list);
}else if(target >= 2){
for(int num: candidates){
stack.push(num);
dfs(candidates, target - num, stack);
stack.pop();
}
}
}
}

【代码4】

​学习参考​

回溯 + 枝减

package com.company;
import java.util.*;

class Solution2 {

private Set<List<Integer>> res = new HashSet<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
dfs(candidates, target, new Stack<>());
return new ArrayList<>(res);
}

public void dfs(int[] candidates, int target, Stack<Integer> stack) {
if (target == 0){
List<Integer> list = new ArrayList<>(stack);
// set 去重用
Collections.sort(list);
res.add(list);
}else if(target >= 2){ //题中规定元素不会小于 2
for (int num : candidates) {
stack.push(num);
dfs(candidates, target - num, stack);
stack.pop();
}
}
}
}
public class Test {
public static void main(String[] args) {
int[] candidates = {2,3,6,7};
int target = 7;
System.out.println(new Solution2().combinationSum(candidates, target).toString());
}
}