18.四数之和

作者: 闭门造折 | 来源:发表于2018-09-27 01:46 被阅读6次

给定一个包含 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]
]

思路:
以为有更巧妙的方法,然而百度发现是两个for循环遍历前两个数,然后将问题转换为15.三数之和
三数之和中用到了双指针法,两端逼近找到所有解。

剔重的方法是,采用三个标志变量
分别表示第一个数、第二个数、第三个数当前值
如果一样则continue,否则做判断

具体代码:

class Solution {
public:
vector<vector<int> > fourSum(vector<int>& nums, int target) {
    vector<vector<int> > res;
    //如果长度不符,直接返回
    if(nums.size() < 4){
        return res;
    }

    //将数组排序
    sort(nums.begin(), nums.end());
    int numi = nums[0] - 1;
    
    //第一层循环,遍历得到第一个数
    for(int i = 0 ; i < nums.size() - 3; i++){
        //标志位相等,则继续往后搜索
        if(numi == nums[i]){
            continue;
        }
        numi = nums[i];
        int numj = nums[i+1] - 1;

        //第二层循环,遍历得到第二个数
        for(int j = i + 1; j < nums.size() - 2; j++){
            //标志位相等,则继续往后搜索
            if(numj == nums[j]){
                continue;
            }
            numj = nums[j];

            //得到后两数字之和
            int twoSum = target - numi - numj;
            int left = j + 1;
            int right = nums.size() - 1;
            int numleft;

            //双指针法逼近中间
            while(left < right){
                //右侧指针过大,指针左移
                if(nums[left] + nums[right] > twoSum){
                    right--;
                    continue;
                }

                //匹配成功,输出,左指针右移
                else if(nums[left] + nums[right] == twoSum){
                    vector<int> tmp;
                    tmp.push_back(numi);
                    tmp.push_back(numj);
                    tmp.push_back(nums[left]);
                    tmp.push_back(nums[right]);
                    res.push_back(tmp);
                }

                //左指针过小,指针右移。与上方匹配成功操作合并
                numleft = nums[left];
                left++;
                //剔重操作,确定左指针不重,则结果中右指针不重
                while(left < right && numleft == nums[left]){
                    left++;
                }
                if(left >= right){
                    break;
                }
                numleft = nums[left];
            }
        }
    }
    return res;
}
};

相关文章

  • 【Leetcode算法题】18. 四数之和

    By Long Luo 18. 四数之和[https://leetcode-cn.com/problems/4su...

  • LeetCode-18 四数之和

    题目:18. 四数之和 难度:中等 分类:数组 解决方案:双指针 今天我们学习第18题四数之和,这是一道中等题。像...

  • 力扣每日一题:18.四数之和

    18.四数之和 https://leetcode-cn.com/problems/4sum/[https://le...

  • 18.四数之和

    18.四数之和 题目链接:https://leetcode-cn.com/problems/4sum/[https...

  • 18. 四数之和

    一、题目原型: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四...

  • 18. 四数之和

    知乎ID: 码蹄疾码蹄疾,毕业于哈尔滨工业大学。小米广告第三代广告引擎的设计者、开发者;负责小米应用商店、日历、开...

  • 18. 四数之和

    18.四数之和 给定一个包含n个整数的数组nums和一个目标值target,判断nums中是否存在四个元素a,b,...

  • 18.四数之和

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,...

  • 18. 四数之和

    一、题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素...

  • 18.四数之和

    自己解法 四数之和解题思路和三数之和类似,不过这个方式是固定前两个数字,后面两个数字用夹逼的方式向中间逼近,这样时...

网友评论

    本文标题:18.四数之和

    本文链接:https://www.haomeiwen.com/subject/hnssoftx.html