LeetCode - 18. 四数之和

掉坑总结:

注意,该题中可能出现target为负数,因此不能像正数一样提前终止循环,比如一个测试用例如下(排序后)[-5, -4, -3, -2, 1, 3,  3,  5], target为-11,不能判断nums[0] = -5已经大于 -11就终止循环,当然如果是正数没有问题。

代码如下(注释部分)

            for (int i = 0; i < n - 3; ++i) {
                if (i > 0 && nums[i] == nums[i - 1]) {
                    // 跳过相同的nums[i]元素,避免解重复
                    continue;
                }
//                if (nums[i] > target) {
//                    // 如果当前nums[i]已经大于target,则后面不可能找到解,跳出循环
//                    break;
//                }
                for (int j = i + 1; j < n - 2; ++j) {
                    if (j > i + 1 && nums[j] == nums[j - 1]) {
                        // 跳过相同的nums[j]元素,避免解重复
                        continue;
                    }
//                    if (nums[i] + nums[j] > target) {
//                        // 如果当前nums[i] + nums[j]已经大于target,则后面不可能找到解,跳出循环
//                        break;
//                    }

 

描述

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:

答案中不可以包含重复的四元组。

示例:

给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/4sum/
 

求解

    class Solution {
    public:
        // 方法一,排序后,两重循环加双指针对撞
        vector<vector<int>> fourSum(vector<int> &nums, int target) {
            std::sort(nums.begin(), nums.end());
            const int n = nums.size();
            vector<vector<int>> res;
            for (int i = 0; i < n - 3; ++i) {
                if (i > 0 && nums[i] == nums[i - 1]) {
                    // 跳过相同的nums[i]元素,避免解重复
                    continue;
                }
//                if (nums[i] > target) {
//                    // 如果当前nums[i]已经大于target,则后面不可能找到解,跳出循环
//                    break;
//                }
                for (int j = i + 1; j < n - 2; ++j) {
                    if (j > i + 1 && nums[j] == nums[j - 1]) {
                        // 跳过相同的nums[j]元素,避免解重复
                        continue;
                    }
//                    if (nums[i] + nums[j] > target) {
//                        // 如果当前nums[i] + nums[j]已经大于target,则后面不可能找到解,跳出循环
//                        break;
//                    }

                    // 双指针对撞
                    int l = j + 1;
                    int r = n - 1;
                    while (l < r) {
                        int sum = nums[i] + nums[j] + nums[l] + nums[r];
                        if (sum < target) {
                            while (l < r && nums[l] == nums[++l]) {} // 跳过重复元素,避免重复解
                        }
                        if (sum > target) {
                            while (l < r && nums[r] == nums[--r]) {} // 跳过重复元素,避免重复解
                        }
                        if (sum == target) {
                            res.push_back(vector<int>{nums[i], nums[j], nums[l], nums[r]});
                            while (l < r && nums[l] == nums[++l]) {} // 跳过重复元素,避免重复解
                            while (l < r && nums[r] == nums[--r]) {} // 跳过重复元素,避免重复解
                        }
                    }
                }
            }
            return res;
        }
    };

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值