Java实现 LeetCode 15 三数之和

15. 三数之和

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。

示例:

给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/3sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
          Arrays.sort(nums);      //对数组进行排序
	HashSet<List<Integer>> Res=new HashSet<>(); //创建HashSet对象
        for(int i=0;i<=nums.length-3;i++){
		if(nums[i]>0)
		     break;    //若数组中的最小数大于0,直接跳出循环
	        int left=i+1;  //左指针
	        int right=nums.length-1;  //右指针
	        while(left<right){
	             int s=nums[i]+nums[left]+nums[right];//三数之和
	             if(s<0){
	            	 left++;       //小于0,左指针右移
	             }else if(s>0){
	            	 right--;     //大于0,右指针左移
	             }else{
	            	 Res.add(Arrays.asList(nums[i],nums[left++],nums[right--]));    //等于0,满足条件,记录下来
	             }
	       }
        }
        return new ArrayList<>(Res);

    }
}
原文地址:https://www.cnblogs.com/a1439775520/p/13075763.html