15. 三数之和

package a6;

/*
* 先排序
* */

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

class Solution {
    public static void main(String[] args) {
        int[] nums = {0,0,0,0,0,0,1,-1};
        System.out.println(new Solution().threeSum(nums));
    }
//    原做法 超时
    /*public List<List<Integer>> threeSum(int[] nums) {
        if(nums == null || nums.length == 0) return Collections.emptyList();
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        int i = 0;
        while(i < nums.length){
            if(i > 0 && i < nums.length - 1 && nums[i] == nums[i-1]){//跳过重复的数
                i++;
                continue;
            }
            int j = i + 1;
            int rightIndex = nums.length - 1;
            while(j < rightIndex){
                if(j > i + 1 && j < nums.length - 1 && nums[j] == nums[j - 1]) {//跳过重复的数
                    j++;
                    continue;
                }
                int leftIndex = j;
                while(leftIndex < rightIndex) {
                    if(rightIndex < nums.length - 1 && nums[rightIndex] == nums[rightIndex + 1]) {//跳过重复的数
                        rightIndex--;
                        continue;
                    }
                    if(nums[leftIndex] + nums[rightIndex] == -nums[i]){//找符合条件的三元组
                        List<Integer> list = new ArrayList<>();
                        Collections.addAll(list, nums[i], nums[leftIndex], nums[rightIndex]);
                        res.add(list);
                    }
                    rightIndex--;
                }
                //对rightIndex初始化
                rightIndex = nums.length - 1;
                j++;
            }
            i++;
        }
        return res;
    }*/

    /*
    * 排序后是小->大所以在遍历找第二个数的时候一定是递增的
    * 所以为了满足第三个数与第二个数相加的值不变,第三个数
    * 一定要递减,否则在固定第一个数的情况下就不存在满足条件
    * 所以我们可以将第二个数与第三个数的遍历同时进行,采用
    * 对撞指针的策略可以将O(n^3)降为O(n^2)
    * */
    public List<List<Integer>> threeSum(int[] nums) {
        if(nums == null || nums.length == 0) return Collections.emptyList();
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for(int i = 0; i < nums.length; i++) {//遍历第一个数
            if(i > 0 && nums[i] == nums[i-1]) continue;//跳过重复的数
            int rightIndex = nums.length - 1;//第三个数
            for(int leftIndex = i + 1; leftIndex < nums.length; leftIndex++) {//遍历第二个数
                if(leftIndex > i + 1 && nums[leftIndex] == nums[leftIndex-1]) continue;//跳过重复的数
                while(leftIndex < rightIndex && nums[leftIndex] + nums[rightIndex] > -nums[i]) {
                    rightIndex--;//两数和大了,减小第三个数,我们是在确定前两个数的情况下找第三个数,所以是移动第三个数
                }
                if(leftIndex == rightIndex) {
                    break;//左右指针对撞了,遍历结束
                }
                if(nums[leftIndex] + nums[rightIndex] == -nums[i]) {//满足条件了
                    List<Integer> list = new ArrayList<>();
                    Collections.addAll(list, nums[i], nums[leftIndex], nums[rightIndex]);
                    res.add(list);
                }
            }
        }
        return res;
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值