题目:

Given an array S of n integers, are there elements abc, 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: 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]
]

题解:

  这个题与3Sum类似,求4Sum就在原基础上再加上一层循环就可以了。这里只给出此种解法思路的其中一个解法。

Solution 1 (36ms)

 class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
set<vector<int>> sv;
sort(nums.begin(), nums.end());
int n = nums.size(); for(int i=; i<n-; i++) {
for(int j=i+; j<n-; j++) {
int k = j+, l = n-;
int a = nums[i], b = nums[j];
while(k<l) {
int c = nums[k], d = nums[l];
if(a+b+c+d == target) {
sv.insert({a,b,c,d});
k++;
l--;
}
else if(a+b+c+d < target) k++;
else l--;
}
}
}
return vector<vector<int>> (sv.begin(),sv.end());
}
};

  还有一种解法,也是利用了3Sum,不过不是再加一层循环,而是直接调用3Sum函数:取nums[i],然后对后续剩余数组元素求3Sum,tar为target - nums[i];

Solution 2 (32ms)

 class Solution {
public:
vector<vector<int> > threeSum(vector<int> &nums, int target) {
set<vector<int>> sv;
sort(nums.begin(), nums.end());
int n = nums.size(); for(int i=; i<n-; i++) {
int a = nums[i];
int j = i+, k = n-;
while(j<k) {
int b = nums[j], c = nums[k];
if(a+b+c == target) {
sv.insert({a,b,c});
j++;
k--;
}
else if(a+b+c > target) k--;
else j++;
}
}
return vector<vector<int>> (sv.begin(),sv.end());
}
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> vv;
sort(nums.begin(), nums.end());
int n = nums.size(); for(int i=; i<n-; i++) {
if(i> && nums[i] == nums[i-]) continue;
//截取剩余数组
vector<int> v(nums.begin()+i+,nums.end());
vector<vector<int>> tmp = threeSum(v, target - nums[i]);
for(int j=; j<tmp.size(); j++) {
tmp[j].insert(tmp[j].begin(), nums[i]);
vv.push_back(tmp[j]);
}
}
return vv;
}
};

  还有一种更为优化的解法,思路是一致的,只是加了一个小技巧:在两个外循环中先判断四个值的和与target的大小,即

  if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;
  if(nums[i]+nums[n-3]+nums[n-2]+nums[n-1]<target) continue;

  通过这两条语句减少了搜索时间,不必进入内循环判断;对于j同理。

Solution 3 (12ms)

 class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
set<vector<int>> sv;
sort(nums.begin(), nums.end());
int n = nums.size();
if(n<) return vector<vector<int>> (sv.begin(),sv.end());
for(int i=; i<n-; i++) {
if(nums[i]+nums[i+]+nums[i+]+nums[i+]>target) break;
if(nums[i]+nums[n-]+nums[n-]+nums[n-]<target) continue;
if(i>&&nums[i]==nums[i-]) continue;
for(int j=i+; j<n-; j++) {
if(j>i+&&nums[j]==nums[j-]) continue;
if(nums[i]+nums[j]+nums[j+]+nums[j+]>target) break;
if(nums[i]+nums[j]+nums[n-]+nums[n-]<target) continue;
int k = j+, l = n-;
int a = nums[i], b = nums[j];
while(k<l) {
int c = nums[k], d = nums[l];
if(a+b+c+d == target) {
sv.insert({a,b,c,d});
k++;
l--;
}
else if(a+b+c+d < target) k++;
else l--;
}
}
}
return vector<vector<int>> (sv.begin(),sv.end());
}
};

Solution 4

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

去重的另一种方法, 使用了algorithm

Solution 5

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int n = nums.size();
vector<vector<int>> res;
sort(nums.begin(), nums.end());
unordered_multimap<int, pair<int, int>> map;
for(int i = ; i < n - ; ++i){
for(int j = i + ; j < n; ++j){
map.insert(make_pair(nums[i] + nums[j], make_pair(i, j)));
}
}
for(auto i = map.begin(); i != map.end(); ++i){
int val = target - i->first;
auto range = map.equal_range(val);
for(auto j = range.first; j != range.second; ++j){
auto a = i->second.first, b = i->second.second;
auto c = j->second.first, d = j->second.second;
if(a != c && a != d && b != c && b != d){
vector<int> tmp = {nums[a], nums[b], nums[c], nums[d]};
sort(tmp.begin(), tmp.end());
res.push_back(tmp);
}
}
}
sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());
return res;
}
};

先缓存两个数的和,注意要使用multimap(from 九章算法)

【LeetCode】018 4Sum的更多相关文章

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

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

  2. 【LeetCode】18. 4Sum (2 solutions)

    4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  3. 【LeetCode】454. 4Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  4. 【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 = ...

  5. 【LeetCode】18. 4Sum

    题目: 思路:这题和15题很像,外层再加一个循环稍作修改即可 public class Solution { public List<List<Integer>> fourSu ...

  6. 【LeetCode】454 4Sum II

    题目: Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are su ...

  7. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  8. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  9. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

随机推荐

  1. iOS表格制作

    由于项目上的需求,需要做一个表格出来,来显示流程状态.刚开始脑子一头雾水,没有一点思路,但是靠着自己的座右铭--“世上无难事,只怕有心人”,克服了所有困难.好,不说了,讲正事. 制作表格,还是需要ta ...

  2. 【BZOJ2780】[Spoj]8093 Sevenk Love Oimaster 广义后缀自动机

    [BZOJ2780][Spoj]8093 Sevenk Love Oimaster Description Oimaster and sevenk love each other.     But r ...

  3. 九度OJ 1207:质因数的个数 (质数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5939 解决:1926 题目描述: 求正整数N(N>1)的质因数的个数. 相同的质因数需要重复计算.如120=2*2*2*3*5,共有 ...

  4. 九度OJ 1199:找位置 (计数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2083 解决:1010 题目描述: 对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12 输出:a,1:a,4 ...

  5. 我的Android进阶之旅------>HTTP Header 详解

    HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务器给与响应.就整个网络资源传 ...

  6. ZOJ - 1505 Solitaire 【双向BFS】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1505 题意 一个8 * 8 的棋盘上面有四个棋子 棋子可以上下左 ...

  7. iOS 在视图控制器里面判断 应用程序的前台 后台切换 UIViewController

    1.时机  用户点击home 键  应用退到后台 再次点击进入前台  在UIViewController里面 控制器如何获取相关的事件? 2.需求 (1)NSTimer   在应用程序进入后台 10秒 ...

  8. ecmobile实现支付宝支付和百度云推送遇到的问题及解决方案(android)

    1.首先检测支付账户是否开通快捷支付服务,如果开通后,那么公钥是否上传(支付宝问题一定要找支付宝客服解决,找其他人没有用,支付宝客服可以帮你分析底层原因) 2.修改app配置文件:alipay_cal ...

  9. 让你快速上手Runtime(转)

    前言 本篇主要介绍Runtime在开发中的一些使用场景,顺便讲解了下MJExtension的底层实现.如果喜欢我的文章,可以关注我微博:袁峥Seemygo,也可以来小码哥,了解下我们的iOS培训课程. ...

  10. uCGUI 按键窗口切换机制(更新篇)

    在之前文章中,讲述了一个低内存使用量的的窗口切换机制.有人会问,低内存使用量是多低呢,我这里举个例子.我有一个项目中使用到本切换机制,128*64的单色屏,初步计算有105个窗口(后面还会增加),总内 ...