package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * https://leetcode.com/problems/combination-sum-ii/
 */
public class Problem40 {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(candidates);
        List<Integer> candidateList = new ArrayList();
        for (int candidate : candidates) {
            candidateList.add(candidate);
        }
        List<Integer> accu = new ArrayList<>();
        Set<List<Integer>> tmpResult = new HashSet<>();
        combination(candidateList, target, 0, 0, accu, tmpResult);
        result.addAll(tmpResult);
        return result;
    }

    private void combination(List<Integer> candidateList, int target, int idx, int sum,
                             List<Integer> accu, Set<List<Integer>> result) {
        if (sum == target) {
            result.add(accu);
            return;
        }
        if (sum > target) {
            return;
        }
        for (int i = idx; i < candidateList.size(); i++) {
            List<Integer> newAccu = new ArrayList<>(accu);
            int val = candidateList.get(i);
            newAccu.add(val);
            combination(candidateList, target, i + 1, sum + val, newAccu, result);
        }
    }
}

@@ -57,21 +57,26 @@
 va.util.
-Array
+Collection
 s;%0Aimpor
@@ -315,19 +315,18 @@
         
-Lis
+Se
 t%3CList%3CI
@@ -338,29 +338,25 @@
 r%3E%3E 
-result
+sets
  = new 
-ArrayLis
+HashSe
 t%3C%3E(
@@ -370,409 +370,109 @@
     
-Arrays.sort(candidates);%0A        List%3CInteger%3E candidateList = new ArrayList();%0A        for (int candidate : candidates) %7B%0A            candidateList.add(candidate);%0A        %7D%0A        List%3CInteger%3E accu = new ArrayList%3C%3E();%0A        Set%3CList%3CInteger%3E%3E tmpResult = new HashSet%3C%3E();%0A        combination(candidateList, target, 0, 0, accu, tmpResult);%0A        result.addAll(tmpResult);%0A        return result
+combinationSum2(candidates, target, 0, new ArrayList%3C%3E(), sets);%0A        return new ArrayList%3C%3E(sets)
 ;%0A  
@@ -508,22 +508,18 @@
 tion
-(List%3CInteger%3E
+Sum2(int%5B%5D
  can
@@ -524,20 +524,17 @@
 andidate
-List
+s
 , int ta
@@ -548,155 +548,214 @@
 nt i
-dx, int sum,%0A                             List%3CInteger%3E accu, Set%3CList%3CInteger%3E%3E result) %7B%0A        if (sum == target) %7B%0A            result.add(accu
+ndex, List%3CInteger%3E accu, Set%3CList%3CInteger%3E%3E sets) %7B%0A        if (target == 0) %7B%0A            List%3CInteger%3E newList = new ArrayList%3C%3E(accu);%0A            Collections.sort(newList);%0A            sets.add(newList
 );%0A 
@@ -799,20 +799,18 @@
 if (
-sum %3E 
 target
+ %3C 0
 ) %7B%0A
@@ -861,17 +861,19 @@
 nt i = i
-d
+nde
 x; i %3C c
@@ -884,19 +884,16 @@
 date
-List.size()
+s.length
 ; i+
@@ -913,126 +913,30 @@
     
-List%3CInteger%3E newAccu = new ArrayList%3C%3E(accu);%0A            int val = candidateList.get(i);%0A            newAccu.add(val
+accu.add(candidates%5Bi%5D
 );%0A 
@@ -957,16 +957,20 @@
 bination
+Sum2
 (candida
@@ -975,20 +975,17 @@
 date
-List
+s
 , target
 , i 
@@ -984,43 +984,85 @@
 rget
-, i + 1, sum + val, newAccu, result
+ - candidates%5Bi%5D, i + 1, accu, sets);%0A            accu.remove(accu.size() - 1
 );%0A 

Update problem 40