package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.List;

/**
 * 78. Subsets
 * Given a set of distinct integers, nums, return all possible subsets.
 * Note: The solution set must not contain duplicate subsets.

 For example,
 If nums = [1,2,3], a solution is:
 [
 [3],
 [1],
 [2],
 [1,2,3],
 [1,3],
 [2,3],
 [1,2],
 []
 ]
 */

public class _78 {

    public static class IterativeSolution {

        public static List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> result = new ArrayList();
            List<Integer> empty = new ArrayList();
            result.add(empty);
            if (nums == null) {
                return result;
            }
            for (int i = 0; i < nums.length; i++) {
                List<List<Integer>> temp = new ArrayList();
                //you'll have to create a new one here, otherwise, it'll throw ConcurrentModificationException.
                for (List<Integer> list : result) {
                    List<Integer> newList = new ArrayList(list);
                    newList.add(nums[i]);
                    temp.add(newList);
                }
                result.addAll(temp);
            }
            return result;
        }

    }

    public static class BacktrackingSolution {

        public List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> result = new ArrayList();
            backtracking(result, new ArrayList(), nums, 0);
            return result;
        }

        void backtracking(List<List<Integer>> result, List<Integer> temp, int[] nums, int start) {
            result.add(new ArrayList(temp));
            for (int i = start; i < nums.length; i++) {
                temp.add(nums[i]);
                backtracking(result, temp, nums, i + 1);
                temp.remove(temp.size() - 1);
            }
        }

    }
}

@@ -399,25 +399,16 @@
 ass 
-Iterative
 Solution
  %7B%0A%0A
@@ -395,36 +395,36 @@
 c class Solution
+1
  %7B%0A
-%0A
         public s
@@ -533,90 +533,8 @@
 ();%0A
-            List%3CInteger%3E empty = new ArrayList();%0A            result.add(empty);%0A
     
@@ -598,32 +598,73 @@
 ;%0A            %7D%0A
+            result.add(new ArrayList());%0A
             for 
@@ -1171,25 +1171,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A    p
@@ -1210,28 +1210,16 @@
 ass 
-Backtracking
 Solution
  %7B%0A%0A
@@ -1214,20 +1214,20 @@
 Solution
+2
  %7B%0A
-%0A
         
@@ -1499,20 +1499,20 @@
 nteger%3E 
-temp
+list
 , int%5B%5D 
@@ -1567,20 +1567,20 @@
 rayList(
-temp
+list
 ));%0A    
@@ -1643,28 +1643,28 @@
             
-temp
+list
 .add(nums%5Bi%5D
@@ -1703,20 +1703,20 @@
 result, 
-temp
+list
 , nums, 
@@ -1743,24 +1743,875 @@
     
-temp.remove(temp
+list.remove(list.size() - 1);%0A            %7D%0A        %7D%0A    %7D%0A%0A    public static class Solution3 %7B%0A        /**%0A         * This is just a slight modification of Solution2, pay close to attention to notice the difference between them.%0A         */%0A        public List%3CList%3CInteger%3E%3E subsets(int%5B%5D nums) %7B%0A            List%3CList%3CInteger%3E%3E result = new ArrayList%3C%3E();%0A            List%3CInteger%3E list = new ArrayList%3C%3E();%0A            result.add(list);%0A            backtracking(result, list, nums, 0);%0A            return result;%0A        %7D%0A%0A        private void backtracking(List%3CList%3CInteger%3E%3E result, List%3CInteger%3E list, int%5B%5D nums, int start) %7B%0A            for (int i = start; i %3C nums.length; i++) %7B%0A                list.add(nums%5Bi%5D);%0A                result.add(new ArrayList%3C%3E(list));%0A                backtracking(result, list, nums, i + 1);%0A                list.remove(list
 .siz
@@ -2640,21 +2640,20 @@
 %7D%0A        %7D%0A
-%0A
     %7D%0A%7D%0A

refactor 78