package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * https://leetcode.com/problems/subsets/
 */
public class Problem78 {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> numList = Arrays.stream(nums)
            .boxed()
            .collect(Collectors.toList());
        Collections.sort(numList);
        Set<List<Integer>> set = new HashSet<>();
        subsets(numList, set);
        result.addAll(set);
        result.add(numList);
        return result;
    }

    private void subsets(List<Integer> nums, Set<List<Integer>> set) {
        if (nums.size() == 0) {
            return;
        }
        for (int i = 0; i < nums.size(); i++) {
            List<Integer> newList = createList(nums, i);
            if (set.contains(newList)) {
                continue;
            }
            set.add(newList);
            subsets(newList, set);
        }
    }

    private List<Integer> createList(List<Integer> nums, int idx) {
        List<Integer> newList = new ArrayList<>();
        for (int i = 0; i < nums.size(); i++) {
            if (i != idx) {
                newList.add(nums.get(i));
            }
        }
        return newList;
    }
}

@@ -61,151 +61,12 @@
 til.
-Arrays;%0Aimport java.util.Collections;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Set;%0Aimport java.util.stream.Collectors
+List
 ;%0A%0A/
@@ -220,22 +220,22 @@
 teger%3E%3E 
-result
+answer
  = new A
@@ -260,286 +260,50 @@
     
-List%3CInteger%3E numList = Arrays.stream(nums)%0A            .boxed()%0A            .collect(Collectors.toList());%0A        Collections.sort(numList);%0A        Set%3CList%3CInteger%3E%3E set = new HashSet%3C%3E();%0A        subsets(numList, set);%0A        result.addAll(set);%0A        result.add(numList
+subsets(nums, 0, new ArrayList%3C%3E(), answer
 );%0A 
@@ -320,14 +320,14 @@
 urn 
-result
+answer
 ;%0A  
@@ -343,16 +343,23 @@
 private 
+static 
 void sub
@@ -363,16 +363,39 @@
 subsets(
+int%5B%5D nums, int index, 
 List%3CInt
@@ -404,16 +404,17 @@
 er%3E 
-nums, Se
+accu, Lis
 t%3CLi
@@ -426,19 +426,22 @@
 teger%3E%3E 
-set
+answer
 ) %7B%0A    
@@ -448,61 +448,42 @@
     
-if (nums.size() == 0) %7B%0A            return;%0A        %7D
+answer.add(new ArrayList%3C%3E(accu));
 %0A   
@@ -492,33 +492,37 @@
    for (int i = 
-0
+index
 ; i %3C nums.size(
@@ -508,38 +508,38 @@
 index; i %3C nums.
-size()
+length
 ; i++) %7B%0A       
@@ -547,500 +547,127 @@
     
-List%3CInteger%3E newList = createList(nums, i);%0A            if (set.contains(newList)) %7B%0A                continue;%0A            %7D%0A            set.add(newList);%0A            subsets(newList, set);%0A        %7D%0A    %7D%0A%0A    private List%3CInteger%3E createList(List%3CInteger%3E nums, int idx) %7B%0A        List%3CInteger%3E newList = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C nums.size(); i++) %7B%0A            if (i != idx) %7B%0A                newList.add(nums.get(i));%0A            %7D%0A        %7D%0A        return newList;
+accu.add(nums%5Bi%5D);%0A            subsets(nums, i + 1, accu, answer);%0A            accu.remove(accu.size() - 1);%0A%0A        %7D
 %0A   

Update problem 78