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. FastAPI 学习之路(五十五)操作Redis

    之前我们分享了操作关系型数据库,具体文章, FastAPI 学习之路(三十二)创建数据库 FastAPI 学习之路(三十三)操作数据库 FastAPI 学习之路(三十四)数据库多表操作 这次我们分享的 ...

  2. [调试笔记] 晚测5 T1 容易题

    众所周知,sbwzx在考试一结束就嚷嚷T1是个sb题.那他为什么调了2小时才调出来呢?快和小编一起看看吧. Sb题:指除了sbwzx别人都能做出来的题 1.CE:震惊!sbwzx竟然连map都不会用, ...

  3. HCNP Routing&Switching之BGP路由属性和优选规则

    前文我们了解了BGP防环机制和路由聚合相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15458110.html:今天我们来聊一聊BGP路由属性和选路规 ...

  4. linux cut

    参考:Linux cut 命令详解_Linux_脚本之家 (jb51.net) 参考:cut命令_Linux cut 命令用法详解:连接文件并打印到标准输出设备上 (linuxde.net)

  5. Python import urllib2 ImportError: No module named 'urllib2'

    python3 import urllib2 import urllib2 ImportError: No module named 'urllib2' python3.3里面,用urllib.req ...

  6. JAVA笔记7__接口应用/Object类/简单工厂模式/静态代理模式/适配器模式

    /** * 接口应用 */ public class Main { public static void main(String[] args) { Person p = new Person(&qu ...

  7. [WPF] 玩玩彩虹文字及动画

    1. 前言 兴致来了玩玩 WPF 的彩虹文字.不是用 LinearGradientBrush 制作渐变色那种,是指每个文字独立颜色那种彩虹文字.虽然没什么实用价值,但希望这篇文章里用 ItemsCon ...

  8. 攻防世界 WEB 高手进阶区 PHP2 Writeup

    攻防世界 WEB 高手进阶区 PHP2 Writeup 题目介绍 题目考点 url 二次解码 index.phps 文件(第一次使用dirsearch可能扫不到,需要加到工具字典里) php 简单语法 ...

  9. 从0到1使用Kubernetes系列(七):网络

    本文是从 0 到 1 使用 Kubernetes 系列第七篇,上一篇<从 0 到 1 使用 Kubernetes 系列(六):数据持久化实战> 介绍了 Kubernetes 中的几种常用储 ...

  10. Go语言核心36讲(Go语言实战与应用六)--学习笔记

    28 | 条件变量sync.Cond (下) 问题 1:条件变量的Wait方法做了什么? 在了解了条件变量的使用方式之后,你可能会有这么几个疑问. 1.为什么先要锁定条件变量基于的互斥锁,才能调用它的 ...