题目描述
java代码
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res=new ArrayList<>();
Arrays.sort(candidates);
backtracking(res,new ArrayList<>(),candidates,target,0);
return res;
}
public void backtracking(List<List<Integer>> res,List<Integer> tmp,int[] candidates,int target,int start){
if(target==0){
res.add(new ArrayList<>(tmp));
}
for(int i=start;i<candidates.length&&candidates[i]<=target;i++){
tmp.add(candidates[i]);
backtracking(res,tmp,candidates,target-candidates[i],i);
tmp.remove(tmp.size()-1);
}
}
}