给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:

答案中不可以包含重复的四元组。

示例:

给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为:

[
   [-1,  0, 0, 1],
   [-2, -1, 1, 2],
   [-2,  0, 0, 2]

]

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/4sum

分析:

跟三数之和的做法是一样的

假设满足要求的数k1<=k2<=k3<=k4

先排序,然后固定k1和k2,通过二分寻找k3和k4

复杂度:O(N*N*log N)

class Solution {
public:
vector<vector<int> > fourSum(vector<int>& a, int t)
{
vector<vector<int> >vv;
vector<int> v;
set<vector<int> > s;
set<vector<int> >::iterator it;
int n=a.size();
if(n<4)
return vv;
sort(a.begin(),a.end());
for(int i=0; i<n-3; i++)
{
for(int j=i+1; j<n-2; j++)
{
int l=j+1;
int h=n-1;
while(l<h)
{
int sum=a[i]+a[j]+a[l]+a[h];
if(sum==t)
{
v.clear();
v.push_back(a[i]);
v.push_back(a[j]);
v.push_back(a[l]);
v.push_back(a[h]);
s.insert(v);
l++;
h--;
}
else if(sum<t)
{
l++;
}
else if(sum>t)
{
h--;
}
}
}
}
if(s.size()!=0)
{
for(it=s.begin(); it!=s.end(); it++)
{
vv.push_back(*it);
}
}
return vv;
}
};

【LeetCode】四数之和【排序,固定k1,k2,二分寻找k3和k4】的更多相关文章

  1. leetcode 四数之和

    这里我们可以考虑将 n 数之和降低为一个数加上 n-1 数之和的问题.依次降低 ,最低是二数之和的问题 ,二数之和问题容易解决.主要在于从 n 到 n-1 的过程需要理解 :下列代码中前几个 if 是 ...

  2. LeetCode:四数之和【18】

    LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...

  3. Leetcode(18)-四数之和

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

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

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

  5. 【LeetCode】四数之和

    [问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...

  6. LeetCode:两数之和、三数之和、四数之和

    LeetCode:两数之和.三数之和.四数之和 多数之和问题,利用哈希集合减少时间复杂度以及多指针收缩窗口的巧妙解法 No.1 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在 ...

  7. 【LeetCode】18.四数之和

    题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...

  8. [LeetCode] 454. 4Sum II 四数之和II

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

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

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

随机推荐

  1. 多项式求逆入门 板题(Luogu P4238)

    下面是代码,推导详见 传送门 模板Code #include <cstdio> #include <cstring> #include <algorithm> us ...

  2. FFT/NTT [51Nod 1028] 大数乘法 V2

    题目链接:51Nod 传送门 没压位,效率会低一点 1.FFT #include <cstdio> #include <cstring> #include <algori ...

  3. tensorflow 2.0 学习(四)

    这次的mnist学习加入了测试集,看看学习的准确率,代码如下 # encoding: utf-8 import tensorflow as tf import matplotlib.pyplot as ...

  4. 使用gitstats分析git 仓库代码

    gitstats 是一个很不错的git 代码提交分析工具,可以帮助我们生成图表统计结果 工具文档信息 gitstats http://gitstats.sourceforge.net/ 安装 使用ce ...

  5. codevs 2780 ZZWYYQWZHZ

    2780 ZZWYYQWZHZ  时间限制: 1 s  空间限制: 32000 KB  题目等级: 青铜 Bronze       题目描述 Description 可爱的小管在玩吹泡泡.忽然,他想到 ...

  6. 「2019-8-11提高模拟赛」女装盛宴 (flag)

    传送门 Solution  基环树+倍增+双指针 第一次因为#define int long long而玄学RE 为什么标程都不用开\(long long\)啊 Code  /*玄学RE 看来defi ...

  7. nRF51822 主从断开连接Reason,HCI ERROR CODE :0x003E

    最近在给一个客户调主从一体的模块,基于S130,距离稍微远一点就会出现连接上后立马又断开连接的现象, 追踪了一下原因,给出的 HCI Error code 是 0x003E,暂且不知道这是什么鬼,查了 ...

  8. 安装OpenStack Queens版本的教程推荐

    为了加深对OpenStack的理解,需要自己分模块安装一次,之前都是用devstack安装,傻瓜式安装虽然方便,但是也减少了我对OpenStack理解的深度. 本人参考如下文档安装成功过 http:/ ...

  9. 第09组 Beta冲刺(1/4)

    队名:软工9组 组长博客:https://www.cnblogs.com/cmlei/ 作业博客:https://edu.cnblogs.com/campus/fzu/SoftwareEngineer ...

  10. IDEA文件查找功能失效(ctrl+shift+N)

    由于断电.蓝屏引起的强制关机等情况,会导出IDEA文件查找功能失效,Enter file name窗口输入的文字显示红色,无法查出需要的文件.  解决方法: 可以点击File,选择Invalidate ...