public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> lists = new ArrayList<>();
if (candidates == null || candidates.length == 0 || target < 0) {
return lists;
}
Arrays.sort(candidates);
List<Integer> list = new ArrayList<>();
process(0, candidates, target, list,lists);
return lists;
}
private void process(int start, int[] candidates, int target, List<Integer> list,List<List<Integer>> lists) {
if (target < 0) {
return;
}
if (target == 0) {
lists.add(new ArrayList<>(list));
} else {
for (int i = start; i < candidates.length; i++) {
//因为这个数和上个数相同,所以从这个数开始的所以情况,在上个数里面都考虑过了,需要跳过
if (i > start && candidates[i] == candidates[i - 1]) {
continue;
}
list.add(candidates[i]);
process(i + 1, candidates, target - candidates[i], list,lists);
list.remove(list.size() - 1);
}
}
}
leet40:组合总和 II
最新推荐文章于 2024-03-30 19:26:36 发布