三数之和
![](https://img2022.cnblogs.com/blog/608798/202211/608798-20221112125205889-440234291.png)
import java.util.*;
public class Solution {
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
//num排序
//复制三个数组a,b,c
//遍历a,遍历b,查找c中是否有0-a-b
//如果a,b,c的下标都不一致,那么新建一个list
//list叠加,过滤相同的list
int len = num.length;
Arrays.sort(num);
int[] a = Arrays.copyOf(num,len);
int[] b = Arrays.copyOf(num, len);
int[] c = Arrays.copyOf(num, len);
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i=0; i<len; i++){
map.put(num[i], i);
}
ArrayList<ArrayList<Integer>> re = new ArrayList<ArrayList<Integer>>();
for(int i=0; i<len; i=map.get(a[i])+1){
for(int j=i+1; j<len; j=map.get(b[j])+1){
//int c_index = Arrays.binarySearch(c,0-a[i]-b[j]);
int r = 0-a[i]-b[j];
if(map.get(r)!=null && map.get(r)>j){
ArrayList<Integer> ls = new ArrayList<Integer>();
ls.add(a[i]);
ls.add(b[j]);
ls.add(r);
re.add(ls);
}
}
}
//list转化为map进行过滤,key的唯一性
for(int k=0; k<re.size(); k++){
}
return re;
}
}