Guest User

Untitled

a guest
Oct 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. //在Combination Sum 基础上,每层递归扫描时排除重复的数字。
  2. public List<List<Integer>> combinationSum2(int[] candidates, int target) {
  3. List<List<Integer>> res = new LinkedList<>();
  4. Arrays.sort(candidates);
  5. backtrack(res, new ArrayList<>(), candidates, target, 0);
  6. return res;
  7. }
  8. public void backtrack(List<List<Integer>> res, List<Integer> tempRes, int[] candidates, int remains, int start){
  9. if(remains<0){
  10. return;
  11. } else if(remains==0){
  12. res.add(new ArrayList<>(tempRes));
  13. } else {
  14. for(int i=start; i<candidates.length; i++){
  15. if(i>start && candidates[i]==candidates[i-1]) continue;
  16. tempRes.add(candidates[i]);
  17. backtrack(res, tempRes, candidates, remains-candidates[i], i+1);
  18. tempRes.remove(tempRes.size()-1);
  19. }
  20. }
  21. }
Add Comment
Please, Sign In to add comment