题目来源:
https://leetcode-cn.com/problems/combination-sum/
题目描述:
代码如下:
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<>();
helper(result, new ArrayList<>(), candidates, target, 0);
return result;
}
public void helper(List<List<Integer>> res, List<Integer> temp,
int candidates[], int target, int start) {
if (target > 0) {
for (int i = start; i < candidates.length && target >= candidates[i]; i++) {
temp.add(candidates[i]);
helper(res, temp, candidates, target - candidates[i], i);
temp.remove(temp.size() - 1);
}
} else if (target == 0) res.add(new ArrayList<Integer>(temp));
}
}