一、题目
给你一个 无重复元素 的整数数组 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
- 2 <= candidates[i] <= 40
- candidates 的所有元素 互不相同
- 1 <= target <= 40
二、代码
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
return process(candidates, target, candidates.length - 1);
}
// 想凑出来的和叫target
// 在arr[0... index]所有的数中自由选择,每一种数任意个。(index+1....) 不用管,认为这个范围已经选择好了
// 凑出target,所有的方案,返回,List<List<Integer>>
public List<List<Integer>> process(int[] arr, int target, int index) {
// 每一层都要提前创建一个ans,会将下层递归返回上来的答案再全部加入到ans中
List<List<Integer>> ans = new ArrayList<>();
// basecase 如果此时target为0,说明当前层已经不需要再去凑数了,目前已经都凑出来了,直接返回
if (target == 0) {
// 需要再加一个List,供上一层添加数据
ans.add(new ArrayList<>());
return ans;
}
// 如果越界了,说明没数可以用了,直接返回ans,这样上一层也就没办法给这一种组合情况追加数了,就相当于丢弃了一种情况
if (index == -1) {
return ans;
}
// 当前尝试放入arr[index]这个数,尝试这个数的可用的所有数量,即i * arr[index] <= target
// arr[index]这个数用i个
for (int i = 0; i * arr[index] <= target; i++) {
// 递归回溯返回下一层的已经找到的结果。此时preAns中所有的组合都是加上i个arr[index]后,总加和就都为target了,即此时preAns中所有组合的加和数都是一样的
List<List<Integer>> preAns = process(arr, target - i * arr[index], index - 1);
// 遍历下一层的结果,将这一层的选择都追加到下一层的结果中
for (List<Integer> list : preAns) {
// arr[index]这个数用i个,所以向每一个结果组合list中加入i个arr[index]
for (int j = 0; j < i; j++) {
list.add(arr[index]);
}
// 将下一层的结果和这一层的选择都加入到这一层的ans中
ans.add(list);
}
}
return ans;
}
}
三、解题思路
利用回溯递归来求解,详细见注释。