# 代码

```public class Solution {
/**
* @param num: Given the candidate numbers
* @param target: Given the target number
* @return: All the combinations that sum to target
*/
public List<List<Integer>> combinationSum2(int[] candidates,
int target) {
List<List<Integer>> results = new ArrayList<>();
if (candidates == null || candidates.length == 0) {
return results;
}

Arrays.sort(candidates);
List<Integer> combination = new ArrayList<Integer>();
helper(candidates, 0, combination, target, results);

return results;
}

private void helper(int[] candidates,
int startIndex,
List<Integer> combination,
int target,
List<List<Integer>> results) {
if (target == 0) {
return;
}

for (int i = startIndex; i < candidates.length; i++) {
if (i != startIndex && candidates[i] == candidates[i - 1]) {
continue;
}
if (target < candidates[i]) {
break;
}
helper(candidates, i + 1, combination, target - candidates[i], results);
combination.remove(combination.size() - 1);
}
}
}```

381 篇文章35 人订阅

0 条评论

## 相关文章

7366

790

3499

3315

3362

2886

2674

### 剑指offer第二天

18.二叉树的镜像 操作给定的二叉树，将其变换为源二叉树的镜像。 ? /** public class TreeNode { int val = 0...

2876

1370

2835