LeetCode算法练习:双指针计算三数之和和四数之和

通过双指针将时间复杂度降一个级别。

java 复制代码
public class TOP {
    //15. 三数之和
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        int n = nums.length;
        if (n < 3) {
            return res;
        }
        Arrays.sort(nums);
        //先确定一个值,再用双指针找另外两个,并去重
        for (int i = 0; i < n; i++) {
            //去除无效值
            if (nums[i] > 0) break;
            //第一个数字去重
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            int left = i + 1;
            int right = n - 1;
            //双指针计算另外两个数字
            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                //不满足条件,缩小窗口
                if (sum > 0) {
                    right--;
                } else if (sum < 0) {
                    left++;
                } else {
                    //找到一个结果
                    res.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    //对另外两个数字去重
                    while (left < right && nums[left] == nums[left + 1]) left++;
                    while (left < right && nums[right] == nums[right - 1]) right--;
                    left++; //移动到不重复区
                    right--;
                }
            }
        }
        return res;
    }

    //18. 四数之和
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<>();
        int n = nums.length;
        if (nums.length < 4) {
            return res;
        }
        //排序来剪枝
        Arrays.sort(nums);
        //先确定2个值,再用双指针找另外两个,并去重
        for (int i = 0; i < n; i++) {
            //去除无效值 (也可以排除[1000000000,1000000000,1000000000,1000000000]溢出情况)
            if (nums[i] > 0 && nums[i] > target) break;
            //第一个数字去重
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            for (int j = i + 1; j < n; j++) {
                //第2个数字去重
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;
                int left = j + 1;
                int right = n - 1;
                //双指针计算另外两个数字
                while (left < right) {
                    //防止int溢出
                    long sum = nums[i] + nums[j] + nums[left] + nums[right];
                    //不满足条件,缩小窗口
                    if (sum > target) {
                        right--;
                    } else if (sum < target) {
                        left++;
                    } else {
                        //找到一个结果
                        res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        //对另外两个数字去重
                        while (left < right && nums[left] == nums[left + 1]) left++;
                        while (left < right && nums[right] == nums[right - 1]) right--;
                        left++;
                        right--;
                    }
                }
            }
        }
        return res;
    }
}
相关推荐
做cv的小昊25 分钟前
【TJU】研究生应用统计学课程笔记(8)——第四章 线性模型(4.1 一元线性回归分析)
笔记·线性代数·算法·数学建模·回归·线性回归·概率论
贾斯汀玛尔斯1 小时前
每天学一个算法--倒排索引(Inverted Index)
算法·inverted-index
小e说说1 小时前
打破偏科困境:这些学习软件助孩子重燃学习热情
算法
月昤昽2 小时前
autoCAD二次开发 4.正多边形与collection区分
算法·c#·二次开发·autocad二次开发
休息一下接着来2 小时前
C++ 固定容量环形队列实现
c++·算法
im_AMBER2 小时前
手撕hot100之矩阵!看完这篇就AC~
javascript·数据结构·线性代数·算法·leetcode·矩阵
笨笨饿2 小时前
#79_NOP()嵌入式C语言中内联汇编宏的抽象封装模式研究
linux·c语言·网络·驱动开发·算法·硬件工程·个人开发
如君愿3 小时前
考研复习 Day 30 | 习题--计算机网络 第五章(运输层 上)、数据结构 图(上)
数据结构·计算机网络·课后习题
weixin_421725263 小时前
C语言中volatile关键字怎么用C语言volatile在多线程中的作用
c语言·数据结构·运算符优先级·变量命名·volatile关键字
风萧萧19993 小时前
问答样例如何在RAG问答中使用?
算法