Combination Sum
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
Last updated
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
Last updated
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if(candidates == null || candidates.length == 0)
return res;
// Arrays.sort(candidates);
backtracking(candidates,res,new ArrayList<>(),target,0);
return res;
}
public void backtracking(int[] candidates, List<List<Integer>> res, List<Integer> list, int remain,int start){
if(remain < 0)
return;
if(remain == 0){
res.add(new ArrayList<>(list));
return;
}
for(int i = start; i < candidates.length; i++){
list.add(candidates[i]);
backtracking(candidates,res,list,remain-candidates[i],i);
list.remove(list.size()-1);
}
}
}