LeetCode Q15 三数之和 双指针法

该博客探讨了如何通过优化算法解决三数之和问题,以避免超时。博主首先介绍了使用三层循环的暴力求解方法,然后详细解释了如何将这种方法优化为双指针算法。优化后的算法首先对输入数组进行排序,然后使用两个指针分别从数组的中间部分开始,根据三数之和与目标值的比较调整指针位置,从而减少循环次数,提高效率。
摘要由CSDN通过智能技术生成

在这里插入图片描述

原题链接

比较直观的方法就是使用三层循环嵌套来暴力迭代找出答案.

 public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        for(int i=0;i<nums.length;i++){
            if(i>0&&nums[i]==nums[i-1])continue;
            for (int j=i+1;j<nums.length;j++){
                if(j>i+1&&nums[j]==nums[j-1])continue;
                for(int k=j+1;k<nums.length;k++){
                    if(k>j+1&&nums[k]==nums[k-1])continue;
                    if(nums[i]+nums[j]+nums[k]==0){
                        List<Integer>temp = new ArrayList<>();
                        temp.add(nums[i]);
                        temp.add(nums[j]);
                        temp.add(nums[k]);
                        res.add(temp);
                    }
                }
            }
        }
        return res;
    }

不过太简单~过不了,会超时.看了一下题解,知道了双指针的方法.三层循环变两层循环.

public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        int n = nums.length;
        Arrays.sort(nums);
        for(int i=0;i<n;i++){
            if(i>0&&nums[i]==nums[i-1])continue;
            int L=i+1;
            int R = n-1;
            while (R>L){
                if(L>i+1&&nums[L]==nums[L-1]){L++;continue;}
                if(R<n-1&&nums[R]==nums[R+1]){R--;continue;}
                if(nums[i]+nums[L]+nums[R]>0) R--;
                else if(nums[i]+nums[L]+nums[R]<0) L++;
                else{List<Integer>temp = new ArrayList<>();
                    temp.add(nums[i]);
                    temp.add(nums[R]);
                    temp.add(nums[L]);
                    res.add(temp);
                    R--;L++;}

            }
        }
        return res;
    }

首先对输入数组进行排序.然后第一层循环比较常规,逐个进行迭代.第二个层循环则是对第一层循环之后的数字进行迭代.并且是使用双指针进行遍历.双指针指向剩余数字的一头一尾.如果三个数字相加>0,那么左指针往右走,让三数之和小一点,向0靠拢.如果三个数字相加<0,那么右指针往左走,让数字之和大一点.

public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        int n = nums.length;
        Arrays.sort(nums);
        for(int i=0;i<n;i++){
            if(i>0&&nums[i]==nums[i-1])continue;
            int L=i+1;
            int R = n-1;
            while (R>L){
                if(L>i+1&&nums[L]==nums[L-1]){L++;continue;}
                if(R<n-1&&nums[R]==nums[R+1]){R--;continue;}
                if(nums[i]+nums[L]+nums[R]>0) R--;
                else if(nums[i]+nums[L]+nums[R]<0) L++;
                else{List<Integer>temp = new ArrayList<>();
                    temp.add(nums[i]);
                    temp.add(nums[R]);
                    temp.add(nums[L]);
                    res.add(temp);
                    R--;L++;}

            }
        }
        return res;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rglkt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值