same as 39
/*
和39一模一样,除了24行和27行
i + 1不能算重复的
if(i > start && cadidates[i - 1] == candidates[i]) continue;
必须i > start
*/
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(candidates);
backtrack(res, new ArrayList<>(), candidates, target, 0);
return res;
}
public void backtrack(List<List<Integer>> res, List<Integer> cur, int[] candidates, int target, int start) {
if(target == 0) {
res.add(new ArrayList<>(cur));
return;
} else if(target < 0) {
return;
} else {
for(int i = start; i < candidates.length; i++) {
if(i > start && candidates[i - 1] == candidates[i])
continue;
cur.add(candidates[i]);
backtrack(res, cur, candidates, target - candidates[i], i + 1);
cur.remove(cur.size() - 1);
}
}
}
}