16-4SUM
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的更多相关文章
- 【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 = ...
- leetcode 整理
1.Two Sum 构造Comparator,KSum 这一类的问题最基本的一题, 解法: 先sort,然后双指针,头尾各一个.进行加逼找值. 对于其余的KSum最终是降次到2次. 如3Sum固定一个 ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 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 ...
- 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 = ...
- 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), ...
- 查找表,Two Sum,15. 3Sum,18. 4Sum,16 3Sum Closest,149 Max points on line
Two Sum: 解法一:排序后使用双索引对撞:O(nlogn)+O(n) = O(nlogn) , 但是返回的是排序前的指针. 解法二:查找表.将所有元素放入查找表, 之后对于每一个元素a,查找 t ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- 【LeetCode】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
- 在Ubuntu 16.10安装mysql workbench报未安装软件包 libpng12-0错误
1.安装mysql workbench,提示未安装软件包 libpng12-0 下载了MySQL Workbench 6.3.8 在安装的时候报错: -1ubu1604-amd64.deb 提示: ...
随机推荐
- kafka-eagle监控界面搭建
kafka-eagle监控界面搭建 一.背景 二 .mac上安装kafka-eagle 1.安装JDK 2.安装eagle 1.下载eagle 2.解压并配置环境变量 3.启用kafka的JMX 4. ...
- WiFi模块选型参考
经常会碰到一些关于wifi模块的咨询,很多刚接触wifi模块的设计人员或者用户,只知道提wifi模块,很难提具体的模块要求!希望通过文章的介绍,会做到有的放矢!咨询时一定要搞清楚自己希望使用什么主芯片 ...
- Python AttributeError: module 'sys' has no attribute 'setdefaultencoding'
Python 3 与 Python 2 有很大的区别,其中Python 3 系统默认使用的就是utf-8编码. 所以,对于使用的是Python 3 的情况,就不需要sys.setdefaultenco ...
- switch中case...用法-c语言
... 表示范围 case 0...4; // error case 5 ... 9; // ok eg 1: char ch = 4; switch(ch) { case 1: printf(& ...
- Markdown使用方式
区块 区块引用在段落开头使用>,后面紧跟一个空格符号 > 区块引用 > XXX > XXX 高级技巧 HTML元素 居中 <center>XXX</cent ...
- prometheus(6)之常用服务监控
监控常用服务 1.tomcat 2.redis 3.mysql 4.nginx 5.mongodb prometheus监控tomcat tomcat_exporter地址 https://githu ...
- github上传和删除文件(三)
上传文件: git init git add * git commit -m "description" //git remote rm origin 或查看当前 git remo ...
- LeetCode 重排链表 OPPO笔试
重排链表 几个关键点: 1. 双指针(快慢指针找中点)(用于反转后一部分) 2. 反转后一部分 (reverse函数) 3. 合并链表 合并的时候在笔试的时候想了一种比我之前想的简单的方法 从slow ...
- PTA 7-3 Windows消息队列 (25分)
PTA 7-3 Windows消息队列 (25分) 消息队列是Windows系统的基础.对于每个进程,系统维护一个消息队列.如果在进程中有特定事件发生,如点击鼠标.文字改变等,系统将把这个消息加到队列 ...
- Leetcode 课程表 C++ 图的深度搜索和广度搜索练习
广度搜索(degree) struct GraphNode{ int label; vector<GraphNode*> neighbours; GraphNode(int x):labe ...