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 提示: ...
随机推荐
- Noip模拟61 2021.9.25
T1 交通 考场上想了一个$NPC$.应该吧,是要求出图里面的所有可行的不重复欧拉路 无数种做法都无法解出,时间也都耗在这个上面的,于是就考的挺惨的 以后要是觉得当前思路不可做,就试着换一换思路,千万 ...
- Pogo-Cow S
这题出在单调队列优化dp里,就离谱好吧...... 对不住了上来先喷一波,不过离谱是确实的 dp的含义也很简单,就是说从j到i的分数最大值 直接上代马,里面说的很详细了 1 #include<b ...
- FreeRTOS学习笔记——FreeRTOS 任务基础知识
RTOS 系统的核心就是任务管理,FreeRTOS 也不例外,而且大多数学习RTOS 系统的工程师或者学生主要就是为了使用RTOS 的多任务处理功能,初步上手RTOS 系统首先必须掌握的也是任务的创建 ...
- 你知道怎么使用Google两步验证保护账户安全吗?
目录 为什么我们需要使用它? 对有些人来说,盗取密码比您想象的更简单 什么是Google两步验证? 多一道安全防线 什么是Google Authenticator ? 使用Google两步验证的好处 ...
- $time $stime $realtime
1,$time The $time system function returns an integer that is a 64-bit time, scaled to the timescale ...
- Linux下文件的三种时间标记:访问时间、修改时间、状态改动时间 (转载)
在windows下,一个文件有:创建时间.修改时间.访问时间. 而在Linux下,一个文件也有三种时间,分别是:访问时间.修改时间.状态改动时间. 两者有此不同,在Linux下没有创建时间的概念,也就 ...
- linux job
通常运行的进程 ctrl-z之后会暂停到后台 bash test.sh Linux-4.15.0-36-generic-x86_64-with-Ubuntu-16.04-xenial #39~16.0 ...
- 电路维修(双端队列 & 最短路)
达达是来自异世界的魔女,她在漫无目的地四处漂流的时候,遇到了善良的少女翰翰,从而被收留在地球上. 翰翰的家里有一辆飞行车. 有一天飞行车的电路板突然出现了故障,导致无法启动. 电路板的整体结构是一个$ ...
- hdu 1083 Courses(二分图最大匹配)
题意: P门课,N个学生. (1<=P<=100 1<=N<=300) 每门课有若干个学生可以成为这门课的代表(即候选人). 又规定每个学生最多只能成为一门课的代 ...
- 攻防世界 Misc 新手练习区 ext3 bugku Writeup
攻防世界 Misc 新手练习区 ext3 bugku Writeup 题目介绍 题目考点 WinHex工具的使用 linux磁盘挂载mount命令 Writeup 下载附件拖进winhex分析一下,查 ...