package com.fishercoder.solutions;

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

public class _18 {

    public static class Solution1 {
        public List<List<Integer>> fourSum(int[] nums, int target) {
            List<List<Integer>> result = new ArrayList();
            if (nums == null || nums.length == 0) {
                return result;
            }
            Arrays.sort(nums);
            for (int i = 0; i < nums.length - 3; i++) {
                if (i > 0 && nums[i - 1] == nums[i]) {
                    continue;
                }
                for (int j = i + 1; j < nums.length - 2; j++) {
                    if (j > i + 1 && nums[j - 1] == nums[j]) {
                        continue;
                    }
                    int left = j + 1;
                    int right = nums.length - 1;
                    while (left < right) {
                        int sum = nums[i] + nums[j] + nums[left] + nums[right];
                        if (sum == target) {
                            result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                            while (left + 1 < right && nums[left] == nums[left + 1]) {
                                left++;
                            }
                            while (right - 1 > left && nums[right] == nums[right - 1]) {
                                right--;
                            }
                            left++;
                            right--;
                        } else if (sum > target) {
                            right--;
                        } else {
                            left++;
                        }
                    }
                }
            }
            return result;
        }
    }
}

@@ -99,19 +99,67 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0Apubl
@@ -1829,11 +1829,982 @@
  %7D%0A    %7D
+%0A%0A    public static class Solution2 %7B%0A        public List%3CList%3CInteger%3E%3E fourSum(int%5B%5D nums, int target) %7B%0A            Arrays.sort(nums);%0A            Set%3CList%3CInteger%3E%3E set = new HashSet%3C%3E();%0A            for (int i = 0; i %3C nums.length - 3; i++) %7B%0A                for (int j = i + 1; j %3C nums.length - 2; j++) %7B%0A                    for (int k = j + 1; k %3C nums.length - 1; k++) %7B%0A                        for (int p = k + 1; p %3C nums.length; p++) %7B%0A                            int sum = nums%5Bi%5D + nums%5Bj%5D + nums%5Bk%5D + nums%5Bp%5D;%0A                            if (sum == target) %7B%0A                                set.add(Arrays.asList(nums%5Bi%5D, nums%5Bj%5D, nums%5Bk%5D, nums%5Bp%5D));%0A                            %7D%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A            List%3CList%3CInteger%3E%3E result = new ArrayList%3C%3E();%0A            for (List%3CInteger%3E each : set) %7B%0A                result.add(each);%0A            %7D%0A            return result;%0A        %7D%0A    %7D
 %0A%7D%0A

add a solution for 18