【题目】

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. Http头 Range、Content-Range(http断点续传原理)

    HTTP头中一般断点下载时才用到Range和Content-Range实体头,Range用户请求头中,指定第一个字节的位置和最后一个字节的位置,如(Range:200-300)Content-Rang ...

  2. Rebound动画框架简单介绍

    Rebound动画框架简单介绍 Android菜鸟一枚,有不对的地方希望大家指出,谢谢. 最近在接手了一个老项目,发现里面动画框架用的是facebook中的Rebound框架,由于以前没听说过,放假时 ...

  3. Android WiFi开发教程(一)——WiFi热点的创建与关闭

    相对于BlueTooth,WiFi是当今使用最广的一种无线网络传输技术, 几乎所有智能手机.平板电脑和笔记本电脑都支持Wi-Fi上网.因此,掌握基本的WiFI开发技术是非常必要的.本教程将围绕一个小D ...

  4. Linux下ntp时间同步

    在root用户下执行 先安装同步时间软件,每台机器执行 yum install -y ntp 然后执行以下命令: crontab -e */10 * * * * /usr/sbin/ntpdate - ...

  5. Foreach嵌套Foreach速度慢优化方案

    有时候这样的效率还可以,但是只要牵涉到操作数据库,那就GAMEOVER.. 最近在维护项目,一个Foreach循环,4分半才能出来结果. 代码: foreach ($content as $key = ...

  6. Hibernate4.3.6 Final+Spring3.0.5配置出错提示及解决方法

    1. Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used ...

  7. 编译-O 选项对性能提升作用

    GCC -O 选项 这个选项控制所有的优化等级.使用优化选项会使编译过程耗费更多的时间,并且占用更多的内存,尤其是在提高优化等级的时候. -O设置一共有五种:-O0.-O1.-O2.-O3和-Os. ...

  8. Linux trace使用入门

    概念 trace 顾名思义追踪信息,可通俗理解为一种高级打印机制,用于debug,实现追踪kernel中函数事件的框架.源代码位于:\kernel\trace\trace.c,有兴趣能够研究 撰写不易 ...

  9. springmvc结合base64存取图片到mysql

    简单介绍: 1.jsp通过MultipartFile上传图片到后台 2.后台把上传的图片通过base64转换成字符串存到mysql 3.从mysql读取图片字符串,通过base64反转成byte数组, ...

  10. 身份证归属地查询免费api接口代码

    描写叙述 :依据身份证编号 查询归属地信息. 身份证实体类: package org.wx.xhelper.model; /** * 身份证实体类 * @author wangxw * @versio ...