4.30更新,已经AC

18. 4Sum My Submissions QuestionEditorial Solution

Total Accepted: 71102 Total Submissions: 299393 Difficulty: Medium

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)

结果:

282 / 282 test cases passed.

Status: Accepted

Runtime: 76 ms

beats:64.95%

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int> > result;
sort(nums.begin(),nums.end());
int n=nums.size();
for(int i=0;i<n-1;++i){
for(int j=i+1;j<n;++j){
int front = nums[i]+nums[j];
int beg = j+1,end = n-1;
while(beg<end){
int tsum = front + nums[beg]+nums[end];
if(tsum==target){
vector<int> vec;
vec.push_back(nums[i]),vec.push_back(nums[j]);
vec.push_back(nums[beg]),vec.push_back(nums[end]);
result.push_back(vec);
while(++beg<end&&nums[beg-1]==nums[beg]);
while(--end>beg&&nums[end+1]==nums[end]);
}
else {
if(tsum<target)beg++;
else end--;
}
}
}
}
sort(result.begin(),result.end());
result.erase(unique(result.begin(),result.end()),result.end());
return result;
}
};

思路:先保存两个整数的和,然后两层循环搞定

平均时间复杂度:O(n2)

还是time limit,期待更好的解法。。。。

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int> > result;
if(nums.size()<4)return result;
sort(nums.begin(),nums.end());
map<int,vector<pair<int,int> > > cache;
for(size_t a=0;a<nums.size();++a){
for(size_t b=0;b<nums.size();++b){
cache[nums[a]+nums[b]].push_back(pair<int,int>(a,b));
}
}
for(int c=0;c<nums.size();++c){
for(size_t d=c+1;d<nums.size();++d){
const int key = target -nums[c]-nums[d];
if(cache.find(key)==cache.end())continue;
const auto&vec =cache[key];
for(size_t k=0;k<vec.size();++k){
if(c<=vec[k].second)continue;
result.push_back({
nums[vec[k].first],nums[vec[k].second],nums[c],nums[d]});
}
}
}
sort(result.begin(),result.end());
result.erase(unique(result.begin(),result.end()),result.end());
return result;
}
};

testcase:

[91277418,66271374,38763793,4092006,11415077,60468277,1122637,72398035,−62267800,22082642,60359529,−16540633,92671879,−64462734,−55855043,−40899846,88007957,−57387813,−49552230,−96789394,18318594,−3246760,−44346548,−21370279,42493875,25185969,83216261,−70078020,−53687927,−76072023,−65863359,−61708176,−29175835,85675811,−80575807,−92211746,44755622,−23368379,23619674,−749263,−40707953,−68966953,72694581,−52328726,−78618474,40958224,−2921736,−55902268,−74278762,63342010,29076029,58781716,56045007,−67966567,−79405127,−45778231,−47167435,1586413,−58822903,−51277270,87348634,−86955956,−47418266,74884315,−36952674,−29067969,−98812826,−44893101,−22516153,−34522513,34091871,−79583480,47562301,6154068,87601405,−48859327,−2183204,17736781,31189878,−23814871,−35880166,39204002,93248899,−42067196,−49473145,−75235452,−61923200,64824322,−88505198,20903451,−80926102,56089387,−58094433,37743524,−71480010,−14975982,19473982,47085913,−90793462,−33520678,70775566,−76347995,−16091435,94700640,17183454,85735982,90399615,−86251609,−68167910,−95327478,90586275,−99524469,16999817,27815883,−88279865,53092631,75125438,44270568,−23129316,−846252,−59608044,90938699,80923976,3534451,6218186,41256179,−9165388,−11897463,92423776,−38991231,−6082654,92275443,74040861,77457712,−80549965,−42515693,69918944,−95198414,15677446,−52451179,−50111167,−23732840,39520751,−90474508,−27860023,65164540,26582346,−20183515,99018741,−2826130,−28461563,−24759460,−83828963,−1739800,71207113,26434787,52931083,−33111208,38314304,−29429107,−5567826,−5149750,9582750,85289753,75490866,−93202942,−85974081,7365682,−42953023,21825824,68329208,−87994788,3460985,18744871,−49724457,−12982362,−47800372,39958829,−95981751,−71017359,−18397211,27941418,−34699076,74174334,96928957,44328607,49293516,−39034828,5945763,−47046163,10986423,63478877,30677010,−21202664,−86235407,3164123,8956697,−9003909,−18929014,−73824245]−236727523

16-4SUM的更多相关文章

  1. 【LeetCode】16. 4Sum

    题目:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  2. leetcode 整理

    1.Two Sum 构造Comparator,KSum 这一类的问题最基本的一题, 解法: 先sort,然后双指针,头尾各一个.进行加逼找值. 对于其余的KSum最终是降次到2次. 如3Sum固定一个 ...

  3. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  4. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  5. 15. 3Sum、16. 3Sum Closest和18. 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

  6. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

  7. 查找表,Two Sum,15. 3Sum,18. 4Sum,16 3Sum Closest,149 Max points on line

    Two Sum: 解法一:排序后使用双索引对撞:O(nlogn)+O(n) = O(nlogn) , 但是返回的是排序前的指针. 解法二:查找表.将所有元素放入查找表, 之后对于每一个元素a,查找 t ...

  8. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  9. 【LeetCode】18. 4Sum 四数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...

  10. 在Ubuntu 16.10安装mysql workbench报未安装软件包 libpng12-0错误

    1.安装mysql workbench,提示未安装软件包 libpng12-0 下载了MySQL Workbench 6.3.8   在安装的时候报错: -1ubu1604-amd64.deb 提示: ...

随机推荐

  1. 2021.8.12考试总结[NOIP模拟37]

    T1 数列 考场上切掉的简单题. $a$,$b$与数列中数的正负值对答案无关.全当作正数计算即可. $exgcd$解未知数系数为$a$,$b$,加和为$gcd(a,b)$的不定方程组,再枚举每个数.如 ...

  2. Java代理:静态代理、JDK动态代理和CGLIB动态代理

    代理模式(英语:Proxy Pattern)是程序设计中的一种设计模式.所谓的代理者是指一个类别可以作为其它东西的接口.代理者可以作任何东西的接口:网络连接.存储器中的大对象.文件或其它昂贵或无法复制 ...

  3. 因为一个小小的Integer问题导致阿里一面没过,遗憾!

    面试题:new Integer(112)和Integer.valueOf(112)的区别 面试官考察点猜想 这道题,考察的是对Integer这个对象原理的理解,关于这道题的变体有很多,我们会一一进行分 ...

  4. DeWeb 简介

    DeWeb是一个可以直接将Delphi程序快速转换为网页应用的工具! 使用DeWeb, 开发者不需要学习HTML.JavaScript.Java.PHP.ASP.C#等新知识,用Delphi搞定一切. ...

  5. Swift-技巧(四)设置照片尺寸和格式

    摘要 平时实现拍照功能时,都是网上一通搜索,整体复制粘贴,自称无脑实现.但是当要求照片是不同的尺寸和格式( JPEG)时,就费力搞照片.其实在设置拍照时,就可以直接设置照片的尺寸和格式,用直接的方法来 ...

  6. 一个疏忽损失惨重!就因为把int改成Integer,第2天被辞了

    1 故事背景 一个程序员就因为改了生产环境上的一个方法参数,把int型改成了Integer类型,因为涉及到钱,结果上线之后公司损失惨重,程序员被辞退了.信不信继续往下看.先来看一段代码: public ...

  7. 难顶!面试官问我G1垃圾收集器

    面试官:要不这次来聊聊G1垃圾收集器? 候选者:嗯嗯,好的呀 候选者:上次我记得说过,CMS垃圾收集器的弊端:会产生内存碎片&&空间需要预留 候选者:这俩个问题在处理的时候,很有可能会 ...

  8. 事件消息生产消费中间件-OSS.DataFlow

    系统重构解耦的过程涉及不同领域服务分拆,或同一服务下实时响应部分和非响应部分分拆,分解后的各部分通过异步消息的流转传递,完成整体的业务逻辑,但是频繁的在业务层面直接调用不同消息队列的SDK,个人感觉不 ...

  9. geoserver控制服务访问权限-类似百度地图的key

    目录 缘起 可行性分析 如何实现key验证访问 如何控制key能访问哪些地图服务? 如何实现服务器ip白名单 流程梳理 申请key 访问地图 实施步骤 拦截器设置 配置key验证规则 配置服务拦截规则 ...

  10. vm扩展磁盘容量后不能启动

    主要原因是,新添加的磁盘空间没有分配,系统识别不出来,导致不能开机. 解决方法: 找到虚拟机的文件路径地址,默认是C:\Users\用户名\Documents\Virtual Machines\Cen ...