题目
给你一个 无重复元素 的整数数组 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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
可以基于题的思路稍微修改,本题是要获得和等于
的所有组合,元素允许重复使用。
本题需要使用一个变量记录数组和。对于每个数,可以考虑选与不选,选的话,将当前数累加到
,另外因为可以使用一个数多次,下一次递归调用时开始考虑的数还是当前数,对应代码中
递归调用时
不变。如果不选,
不变,下一次递归调用时开始考虑的数为下一个数,对应代码中
递归调用时
加
。
代码
代码一
class Solution {
List<List<Integer>> ans;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
ans = new ArrayList<>();
int n = candidates.length;
dfs(0, n, 0, target, candidates, new ArrayDeque<>());
return ans;
}
private void dfs(int begin, int n, int sum, int target, int[] candidates, Deque<Integer> path) {
if (sum == target) {
ans.add(new ArrayList<>(path));
return;
}
if (sum > target || begin == n) {
return;
}
// 当前数选择一次或多次,所以下次开始考虑的下标还是begin
path.offerLast(candidates[begin]);
dfs(begin, n, sum + candidates[begin], target, candidates, path);
path.pollLast();
// 不选择当前数,所以下次开始考虑的下标为begin+1
dfs(begin + 1, n, sum, target, candidates, path);
}
}
代码二 对代码一剪枝
上述代码可以通过对输入数组进行排序可以剪枝,减少不必要的搜索。
对输入数组升序排列,如果sum加当前数已经超过target了,那么就不需要再考虑后面的数了,因为一定也会大于target。
class Solution {
List<List<Integer>> ans;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
// 排序是为了剪枝
Arrays.sort(candidates);
ans = new ArrayList<>();
int n = candidates.length;
dfs(0, n, 0, target, candidates, new ArrayDeque<>());
return ans;
}
private void dfs(int begin, int n, int sum, int target, int[] candidates, Deque<Integer> path) {
if (sum == target) {
ans.add(new ArrayList<>(path));
return;
}
if (sum > target || begin == n) {
return;
}
// 加了这个判断可以达到剪枝效果
if (sum + candidates[begin] <= target) {
// 当前数选择一次或多次,所以下次开始考虑的下标还是begin
path.offerLast(candidates[begin]);
dfs(begin, n, sum + candidates[begin], target, candidates, path);
path.pollLast();
}
// 不选择当前数,所以下次开始考虑的下标为begin+1
dfs(begin + 1, n, sum, target, candidates, path);
}
}
代码三
另一种写法,内部写成了循环形式,也是依次考虑每个数,同样为了避免重复,下次循环开始只考虑当前数后面的数。
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
int n = candidates.length;
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++) {
path.offerLast(candidates[i]);
dfs(candidates, target, n, sum + candidates[i], i, path, ans);
path.pollLast();
}
}
}
代码四 对代码三剪枝
剪枝思路和代码二对代码一的剪枝一样。
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();
}
}
}