【题目】

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)

【题意】

    给定一个数组,从中取4个数使它们的和等于target, 找出全部的组合。
    不能有反复;
    且组合中各个数升序排列;

【思路】

    思路和3Sum的思路全然同样,先确定第一个数,剩下的就又变成3Sum问题了

【代码】

class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
vector<vector<int> >result;
int size=num.size();
if(size<4)return result;
sort(num.begin(), num.end()); //排序
for(int p1=0; p1<size-3; p1++){
if(p1!=0&&num[p1]==num[p1-1])continue; //第一个数排重
for(int p2=p1+1; p2<size-2; p2++){
if(p2!=p1+1 && num[p2]==num[p2-1])continue; //第二个数排重
int p3=p2+1;
int p4=size-1;
while(p3<p4){
if(p3!=p2+1 && num[p3]==num[p3-1]){p3++;continue;} //第三个数排重
int sum=num[p1]+num[p2]+num[p3]+num[p4];
if(sum==target){
vector<int> quadruplet;
quadruplet.push_back(num[p1]);
quadruplet.push_back(num[p2]);
quadruplet.push_back(num[p3]);
quadruplet.push_back(num[p4]);
result.push_back(quadruplet);
p3++;p4--;
}
else if(sum>target)p4--;
else p3++;
}
}
}
return result;
}
};

LeetCode 017 4Sum的更多相关文章

  1. [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 ...

  2. LeetCode——18. 4Sum

    一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...

  3. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

  4. [LeetCode] 18. 4Sum ☆☆

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

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

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

  6. [LeetCode] 18. 4Sum 四数之和

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

  7. leetcode 日记 4sum java

    整体思路同之前的一样,依然采取降低维度的方式进行 public List<List<Integer>> solution(int nums[], int target) { L ...

  8. 【leetcode】4Sum

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

  9. LeetCode 18. 4Sum (四数之和)

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

随机推荐

  1. ListView设置某一项item的文本居中

    使用ListView和volley写了一个使用网络获取天气的demo ListView中Item的文本模式都是左侧对齐 我这边需要一些标题文本居中对齐 网上也找不到示例,不过找到了getView这个函 ...

  2. static再次深入理解

    在java中,栈中存放的是用来保存方法运行时状态的栈帧,存储了局部变量表,操作数栈等,而方法区存放的是已加载的类的基本信息.常量.静态变量等.

  3. eclipse中mybatis generator插件的安装与使用,实现自动生成代码

    git地址:https://github.com/mybatis/generator 下载后解压: 选择任意一个版本的jar放到eclipse的features目录下即可 选择任意一个版本的jar放到 ...

  4. TestNG的參数化測试、共享线程池配置、參数默认值配置

    在使用TestNG进行測试时,常常会使用到一些參数化配置,比方数据库.连接池.线程池数. 使用TestNG的參数@Parameter注解进行自己主动化读取 原创文章,版权全部.同意转载,标明出处:ht ...

  5. HTML5 Canvas 绘制新西兰国旗

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  6. RobotFramework-Selenium2Library--关键字

    Selenium2Library用户关键字 *** Settings *** Library Selenium2Library *** Keywords *** Checkbox应该不被选择 [Arg ...

  7. 使用Nginx的proxy_cache缓存功能取代Squid

    Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302 ...

  8. S:33184777

    https://github.com/wjlWork/Crawl/tree/da0b63bc43bd7c238963f834a11df2581cecfb9c https://github.com/wh ...

  9. ubantu 下 修改mysql 默认编码

    启动mysql后,以root登录mysql root@Eadgar-virtual-machine:~# mysql -uroot -proot mysql> show variables li ...

  10. Ubuntu 14.04lts安装vncserver

    之前有在centos上安装过非常多次vncserver,也写过一个centos 7上的安装文档.近来常识了好几次在ubuntu上安装都没有成功,这次最终搞定了.ubuntu自带的桌面是unity.这个 ...