三数之和与四数之和 | LeetCode-15 | LeetCode-18 | 双指针 | 降维 | 哈希集合 | Java | 详细注释

🙋大家好!我是毛毛张!
🌈个人首页: 神马都会亿点点的毛毛张
🍏去重和🍓降维是这两道题的关键!

📌LeetCode链接:15. 三数之和
📌LeetCode链接:18. 四数之和

题目1:三数之和🍇

1.题目描述🍉

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != ji != kj != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请你返回所有和为 0 且不重复的三元组。

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

示例 1:

输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
解释:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。
不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。
注意,输出的顺序和三元组的顺序并不重要。

示例 2:

输入:nums = [0,1,1]
输出:[]
解释:唯一可能的三元组和不为 0 。

示例 3:

输入:nums = [0,0,0]
输出:[[0,0,0]]
解释:唯一可能的三元组和为 0 。

提示:

  • 3 <= nums.length <= 3000
  • − 1 0 5 < = n u m s [ i ] < = 1 0 5 -10^5 <= nums[i] <= 10^5 105<=nums[i]<=105

2.题解🍋

2.1 双指针🍑

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        // 对数组进行排序,方便后续使用双指针方法寻找满足条件的三元组
        Arrays.sort(nums);
        // 用于存放所有满足条件的三元组结果
        List<List<Integer>> resultList = new ArrayList<>();
        int n = nums.length;

        // 第一层循环,选择第一个数
        for (int first = 0; first < n - 2; first++) {
            // 如果当前数字大于0,则不可能有三元组和为0,因为数组已排序
            if (nums[first] > 0) break;
            // 跳过重复的数字以避免添加重复的三元组
            if (first > 0 && nums[first] == nums[first - 1]) continue;
            // 定义双指针,second 从 first+1 开始,third 从数组末尾开始
            int second = first + 1, third = n - 1;

            // 双指针查找当前first对应的所有满足条件的三元组
            while (second < third) {
                // 计算三数之和
                int sum = nums[first] + nums[second] + nums[third];
                
                // 如果和为0,找到一个满足条件的三元组
                if (sum == 0) {
                    List<Integer> list = new ArrayList<>();
                    list.add(nums[first]);
                    list.add(nums[second]);
                    list.add(nums[third]);
                    resultList.add(list);
                    //resultList.add(Arrays.asList(nums[first], nums[second], nums[third]));

                    // 去重:移动second和third指针以跳过重复的数字
                    while (second < third && nums[second] == nums[second + 1]) second++;
                    while (second < third && nums[third] == nums[third - 1]) third--;
                    
                    // 移动指针,继续寻找其他可能的三元组
                    second++;
                    third--;
                } else if (sum > 0) {
                    // 如果和大于0,说明third指向的值太大,移动third指针减小总和
                    third--;
                } else {
                    // 如果和小于0,说明second指向的值太小,移动second指针增加总和
                    second++;
                }
            }
        }
        // 返回所有找到的三元组列表
        return resultList;
    }
}

题目2:四数之和🍎

1.题目描述🍍

给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):

  • 0 <= a, b, c, d < n
  • abcd 互不相同
  • nums[a] + nums[b] + nums[c] + nums[d] == target

你可以按 任意顺序 返回答案 。

示例 1:

输入:nums = [1,0,-1,0,-2,2], target = 0
输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

示例 2:

输入:nums = [2,2,2,2,2], target = 8
输出:[[2,2,2,2]]

提示:

  • 1 <= nums.length <= 200
  • − 1 0 9 < = n u m s [ i ] < = 1 0 9 -10^9 <= nums[i] <= 10^9 109<=nums[i]<=109
  • − 1 0 9 < = t a r g e t < = 1 0 9 -10^9 <= target <= 10^9 109<=target<=109

2.题解🥝

2.1 双指针🥥

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        // 对数组进行排序,方便后续使用双指针方法寻找满足条件的四元组
        Arrays.sort(nums);
        // 用于存放所有满足条件的四元组结果
        List<List<Integer>> resultList = new ArrayList<>();
        int n = nums.length;

        // 第一层循环,选择第一个数
        for (int first = 0; first < n - 3; first++) {
            // 跳过重复的数字以避免添加重复的四元组
            if (first > 0 && nums[first] == nums[first - 1]) continue;
            // 第二层循环,选择第二个数
            for (int second = first + 1; second < n - 2; second++) {
                // 跳过重复的数字以避免添加重复的四元组
                if (second > first + 1 && nums[second] == nums[second - 1]) continue;
                // 定义双指针,third 从 second+1 开始,fourth 从数组末尾开始
                int third = second + 1;
                int fourth = n - 1;
                // 双指针查找当前first和second对应的所有满足条件的四元组
                while (third < fourth) {
                    // 计算四数之和,使用long以防止整数溢出
                    long sum = (long) nums[first] + nums[second] + nums[third] + nums[fourth];

                    // 如果和等于目标值,找到一个满足条件的四元组
                    if (sum == target) {
                        List<Integer> list = new ArrayList<>();
                        list.add(nums[first]);
                        list.add(nums[second]);
                        list.add(nums[third]);
                        list.add(nums[fourth]);
                        resultList.add(list);
                        

                        // 去重:移动third和fourth指针以跳过重复的数字
                        while (third < fourth && nums[third] == nums[third + 1]) third++;
                        while (third < fourth && nums[fourth] == nums[fourth - 1]) fourth--;

                        // 移动指针,继续寻找其他可能的四元组
                        third++;
                        fourth--;
                    } else if (sum > target) {
                        // 如果和大于目标值,说明fourth指向的值太大,移动fourth指针减小总和
                        fourth--;
                    } else {
                        // 如果和小于目标值,说明third指向的值太小,移动third指针增加总和
                        third++;
                    }
                }
            }
        }

        // 返回所有找到的四元组列表
        return resultList;
    }
}
  • 优化:
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        // 对数组进行排序,方便后续使用双指针方法寻找满足条件的四元组
        Arrays.sort(nums);
        // 用于存放所有满足条件的四元组结果
        List<List<Integer>> resultList = new ArrayList<>();
        int n = nums.length;

        // 第一层循环,选择第一个数
        for (int first = 0; first < n - 3; first++) {
            // 1级剪枝处理
            if(nums[first] > target && nums[first] >0) break;
            // 跳过重复的数字以避免添加重复的四元组
            if (first > 0 && nums[first] == nums[first - 1]) continue;
            // 第二层循环,选择第二个数
            for (int second = first + 1; second < n - 2; second++) {
                // 2级剪枝处理
                if (nums[first] + nums[second] > target && nums[first] + nums[second] >= 0) break;
                // 跳过重复的数字以避免添加重复的四元组
                if (second > first + 1 && nums[second] == nums[second - 1]) continue;
                // 定义双指针,third 从 second+1 开始,fourth 从数组末尾开始
                int third = second + 1;
                int fourth = n - 1;
                // 双指针查找当前first和second对应的所有满足条件的四元组
                while (third < fourth) {
                    // 计算四数之和,使用long以防止整数溢出
                    long sum = (long) nums[first] + nums[second] + nums[third] + nums[fourth];

                    // 如果和等于目标值,找到一个满足条件的四元组
                    if (sum == target) {
                        resultList.add(Arrays.asList(nums[first], nums[second], nums[third], nums[fourth]));

                        // 去重:移动third和fourth指针以跳过重复的数字
                        while (third < fourth && nums[third] == nums[third + 1]) third++;
                        while (third < fourth && nums[fourth] == nums[fourth - 1]) fourth--;

                        // 移动指针,继续寻找其他可能的四元组
                        third++;
                        fourth--;
                    } else if (sum > target) {
                        // 如果和大于目标值,说明fourth指向的值太大,移动fourth指针减小总和
                        fourth--;
                    } else {
                        // 如果和小于目标值,说明third指向的值太小,移动third指针增加总和
                        third++;
                    }
                }
            }
        }

        // 返回所有找到的四元组列表
        return resultList;
    }
}
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

神马都会亿点点的毛毛张

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

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

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

打赏作者

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

抵扣说明:

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

余额充值