给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组
import jdk.nashorn.internal.runtime.arrays.ArrayIndex;
import java.util.*;
/**
* 3sum之和,给定一个包含 n 个整数的数组 nums,
* 判断 nums 中是否存在三个元素 a,b,c ,
* 使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组
*/
public class Demo50 {
/**
* 排序 去重
* @param nums
* @return
*/
/**
* 解题思路:先对数组进行排序,之后数组是有序的。3指针遍历,
* k来表示第一个数字,其他两个数字和为target=0-arr[k];
* 然后在[k..leng-2]区间,在k后面寻找两个数字之和为为target,如果为target在去重,如果不为target
* 如果比target小,则需要i往右移动,因为数组是排好序的。
* 如果比target大,则需要j往左移动,
* @param nums
* @return
*/
public static ArrayList<List<Integer>> threeSum(int nums[]) {
ArrayList<List<Integ