# LeetCode 18：4Sum

Given an array S of n integers, are there elements abc, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note: The solution set must not contain duplicate quadruplets.

```For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
[-1,  0, 0, 1],
[-2, -1, 1, 2],
[-2,  0, 0, 2]
]
```

```package com.maoxiaomeng;

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

/**
* @author lihui
* @date 2018/3/26 12:31
*/

public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> tempList = new ArrayList<List<Integer>>();
int len = nums.length;
if (nums == null || len < 4) {
return tempList;
}
Arrays.sort(nums);
/**
* {1, 0, -1, 0, -2, 2};
* {-2, -1, 0, 0, 1, 2};
* i = 0, j = i + 1, left = j + 1, right = len - 1
*/
for (int i = 0; i < len - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < len - 2; j++) {
if (nums[j] == nums[j - 1]) {
continue;
}
int left = j + 1;
int right = len - 1;
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum > 0) {
right--;
} else if (sum < 0) {
left++;
} else {
List<Integer> myList = new ArrayList<Integer>();
left++;
right--;
}
while (left < right && nums[left] == nums[left - 1]) {
left++;
}
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
}
}
}
return tempList;
}
}```

```package com.maoxiaomeng;

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

/**
* @author lihui
* @date 2018/3/26 12:31
*/

public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> tempList = new ArrayList<List<Integer>>();
int len = nums.length;
if (nums == null || len < 4) {
return tempList;
}
Arrays.sort(nums);
/**
* {1, 0, -1, 0, -2, 2};
* {-2, -1, 0, 0, 1, 2};
* i = 0, j = i + 1, left = j + 1, right = len - 1
*/
for (int i = 0; i < len - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < len - 2; j++) {
if (nums[j] == nums[j - 1]) {
continue;
}
int left = j + 1;
int right = len - 1;
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum > 0) {
right--;
} else if (sum < 0) {
left++;
} else {
List<Integer> myList = new ArrayList<Integer>();
left++;
right--;
while (left < right && nums[left] == nums[left - 1]) {
left++;
}
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
}
}
}
}
return tempList;
}
}```

while里做了误操作

1：去掉j循环体里continue判断

3：更蛋疼的是，题目要求和为target，而不是0，眼神不好

```package com.maoxiaomeng;

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

/**
* @author lihui
* @date 2018/3/26 12:31
*/

public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> tempList = new ArrayList<List<Integer>>();
int len = nums.length;
if (nums == null || len < 4) {
return tempList;
}
Arrays.sort(nums);
/**
* {1, 0, -1, 0, -2, 2};
* {-2, -1, 0, 0, 1, 2};
* i = 0, j = i + 1, left = j + 1, right = len - 1
*/
for (int i = 0; i < len - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < len - 2; j++) {
int left = j + 1;
int right = len - 1;
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
List<Integer> myList = new ArrayList<Integer>();
if (! tempList.contains(myList)) {