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;
    }
}
相关推荐
JieE21210 小时前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack2018 小时前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树20 小时前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2121 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2121 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术2 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦2 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
用户497863050732 天前
(一)小红的数组操作
算法·编程语言
怕浪猫2 天前
Electron 系列文章封面图
算法·架构·前端框架