LeetCode 18 4Sum(4个数的和)

简介:

翻译

给定一个有n个数字的数组S,在S中是否存在元素a,b,c和d的和恰好满足a + b + c + d = target。

找出数组中所有的不想等的这四个元素,其和等于target。

备注:

在(a,b,c,d)中的元素必须从小到大排列。(a ≤ b ≤ c ≤ d)
其结果必须不能够重复。

例如,给定S = {1 0 -1 0 -2 2},target = 0。
一个结果集为:
(-1,  0, 0, 1)
(-2, -1, 1, 2)
(-2,  0, 0, 2)

原文

Given an array S of n integers, 
are there elements a, b, c, and d in S such that a + b + c + d = target? 
Find all unique quadruplets in the array which gives the sum of target.

Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.

For example, given array S = {1 0 -1 0 -2 2}, and target = 0.

A solution set is:
(-1,  0, 0, 1)
(-2, -1, 1, 2)
(-2,  0, 0, 2)

代码

具体的方法和前面两道题一样,我就不再赘述了。

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums,int target) {
        sort(nums.begin(), nums.end());
        vector<vector<int>> result;

        int len = nums.size();      
        for (int current = 0; current < len - 3;current++)
        {
            for(int second = current+1;second<len-2;second++)
            {
                int front = second + 1, back = len - 1;
                while (front < back)
                {
                    if (nums[current]+nums[second] + nums[front] + nums[back]  < target)
                        front++;
                    else if (nums[current] +nums[second]+ nums[front] + nums[back] > target)
                        back--;
                    else
                    {
                        vector<int> v(4);
                        v[0]=nums[current];
                        v[1]=nums[second];
                        v[2]=nums[front];
                        v[3]=nums[back];
                        result.push_back(v);
                        do {
                            front++;
                        } while (front < back&&nums[front - 1] == nums[front]);
                        do {
                            back--;
                        } while (front < back&&nums[back + 1] == nums[back]);
                    }
                } 
                while(second < len-2&&nums[second+1]==nums[second])
                    second++;
            }                               
            while (current < len - 3 && nums[current + 1] == nums[current])
                current++;
        }                                  
        return result;
    }
};

和本道题关联密切的题目推荐:

传送门:LeetCode 15 3Sum(3个数的和)
传送门:LeetCode 16 3Sum Closest(最接近的3个数的和)

目录
相关文章
|
7月前
|
域名解析 缓存 运维
网站测速:纯干货无广告无敏感内容
网站测速是评估访问性能的关键手段,涵盖DNS解析、TTFB、首屏/整站加载等核心指标。KKCE作为专业在线工具,提供多节点、全链路检测与可视化诊断,助力个人站长与企业快速定位瓶颈、科学优化体验。(239字)
410 1
|
12月前
|
存储 数据采集 缓存
2026年阿里云经济型e实例(ecs.e-c1m2.large)2核4G云服务器优惠活动及性能测评说明
阿里云经济型e实例(ecs.e-c1m2.large)2核4G配置,支持按使用流量或按固定带宽两种公网计费方式,搭配20G起ESSD Entry云盘,是主打高性价比的入门级选择。其核心特点是成本可控、配置灵活,能适配个人开发者的轻量项目与小微企业的基础业务需求,既无需为闲置带宽付费,也可根据流量波动选择合适计费模式。以下从优惠活动价格、性能表现、适用场景及避坑要点四方面,用通俗语言详细解析。
|
JavaScript 前端开发 API
前端工程化:有效地进行拼写检查
前端工程化:有效地进行拼写检查
521 1
|
设计模式
23种设计模式
23种设计模式主要分为三大类:创建型模式、结构型模式和行为型模式。
276 0
Yaml配置文件-教学+实战
Yaml配置文件-教学+实战
684 0
|
Python
python实现简单的snake game!
python实现简单的snake game!
288 0
|
云安全 消息中间件 弹性计算
阿里云飞天会员体系福利及准入门槛说明
阿里云推出飞天会员,加入飞天会员可享受购买云服务器等等产品飞天会员价,云帮云来详细说下阿里云飞天会员等级及加入门槛
1593 0
阿里云飞天会员体系福利及准入门槛说明
|
前端开发 测试技术 API
fiddler mock ==> AutoResponder
背景 做过测试的同学,肯定都听过fiddler的大名,抓包工具,app抓包 下载传送门(https://www.telerik.com/download/fiddler) 抓包使用这里就不复述了,这次介绍mock功能,也就是AutoResponder功能 我们在测试web前端的时候,前端的数据展示都是来源于后端接口的response。
1226 0