【题目】

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. OnTouchListener

    1.布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  2. 盘点支持Orchard的.NET 4.5虚拟主机(虚拟空间)

    Orchard作为.NET社区最为先进的开源项目之一,已经被越来越多的人使用, 由于它使用了最时髦的微软技术栈(.NET4.5),因此在 Win2008+和IIS7+ 的环境上运行最为完美, 而win ...

  3. 调整type="file"时的input的

    <input type="file"> 在ie下的视图如下 而在firefox下的是 一般为了界面美化的效果,会将其设置为透明,然后覆盖一个<a href = & ...

  4. uiaotumator ui測试 高速调试

    1. uiaotumator ui測试 Demo.java package uiautomatorDemo1; import java.io.File; import android.graphics ...

  5. Failed to read artifact descriptor for avalon-framework:avalon-framewor

    在工程中,遇到了这个问题,百度了好久并没有满意的解决方案. 网上有一种办法是: 一.修改.m2/repository/avalon-framework/avalon-framework-api/里所有 ...

  6. Win7如何解决精简版的迅雷7无法运行

    网上下载msvcp71.dll和msvcr71.dll把文件放到System32目录下即可   http://www.baidu.com/s?wd=msvcp71.dll&ie=utf-8&a ...

  7. NSIS隐藏窗体标题栏自带的button(最大化,最小化,关闭X)

    这个问题实在八月份逛csdn论坛的时候偶然遇到的,当时比較好奇楼主为啥要隐藏关闭button.就顺口问了下,结果楼主已经弃楼.未给出原因,猜着可能是为了做自己定义页面美化,无法改变按纽外观之类的,后来 ...

  8. windows ce.net开发概述

    依据开发所处的层次以及开发工具的不同,能够将嵌入式系统开发分为系统开发和应用开发. 系统开发所涉及的内容包含三个方面:系统定制.驱动程序开发.操作系统一致(BSP开发). 一系统开发 (1)      ...

  9. Android---63---Android中的动画效果

    Android中有四种动画效果: AlphaAnimation:透明度动画效果 ScaleAnimation:缩放动画效果 TranslateAnimation:位移动画效果 RotateAnimat ...

  10. iOS 瀑布流封装

    代码地址如下:http://www.demodashi.com/demo/12284.html 一.效果预览 功能描述:WSLWaterFlowLayout 是在继承于UICollectionView ...