# 15.三数之和

class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list=new ArrayList<>();
Arrays.sort(nums);
if(nums.length==0 || nums[0]>0){
return list;
}
if(nums.length>=3){
for(int i=0;i<nums.length-2;i++){
int k=i+1;
int z=nums.length-1;
while(k<z) {
List<Integer> list1=new ArrayList<>();
if (nums[i] + nums[k] + nums[z] > 0) {
z--;
}else if(nums[i] + nums[k] + nums[z] <0){
k++;
}else{
if(!list1.isEmpty()){
}
while (k < z && nums[k] == nums[k + 1]) k++;
while (k < z && nums[z] == nums[z - 1]) z--;

k++;
z--;
}
}
}
}
list.clear();
return list;
}
}

public static List<List<Integer>> threeSum(int[] nums) {
if (nums.length < 3)
return Collections.emptyList();
List<List<Integer>> res = new ArrayList<>();
int minValue = Integer.MAX_VALUE;
int maxValue = Integer.MIN_VALUE;
int negSize = 0;
int posSize = 0;
int zeroSize = 0;
for (int v : nums) {
if (v < minValue)
minValue = v;
if (v > maxValue)
maxValue = v;
if (v > 0)
posSize++;
else if (v < 0)
negSize++;
else
zeroSize++;
}
if (zeroSize >= 3)
if (negSize == 0 || posSize == 0)
return res;
if (minValue * 2 + maxValue > 0)
maxValue = -minValue * 2;
else if (maxValue * 2 + minValue < 0)
minValue = -maxValue * 2;

int[] map = new int[maxValue - minValue + 1];
int[] negs = new int[negSize];
int[] poses = new int[posSize];
negSize = 0;
posSize = 0;
for (int v : nums) {
if (v >= minValue && v <= maxValue) {
if (map[v - minValue]++ == 0) {
if (v > 0)
poses[posSize++] = v;
else if (v < 0)
negs[negSize++] = v;
}
}
}
Arrays.sort(poses, 0, posSize);
Arrays.sort(negs, 0, negSize);
int basej = 0;
for (int i = negSize - 1; i >= 0; i--) {
int nv = negs[i];
int minp = (-nv) >>> 1;
while (basej < posSize && poses[basej] < minp)
basej++;
for (int j = basej; j < posSize; j++) {
int pv = poses[j];
int cv = 0 - nv - pv;
if (cv >= nv && cv <= pv) {
if (cv == nv) {
if (map[nv - minValue] > 1)
} else if (cv == pv) {
if (map[pv - minValue] > 1)