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:

  • 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.
  1. For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
  2.  
  3. A solution set is:
  4. (-1, 0, 0, 1)
  5. (-2, -1, 1, 2)
  6. (-2, 0, 0, 2)
    3sum一样
  1. class Solution {
  2. public:
  3. vector<vector<int> > fourSum(vector<int> &num, int target) {
  4. vector<vector<int> > res;
  5. int n = num.size();
  6. if( n < ) return res;
  7. sort(num.begin(),num.end());
  8. for(int i = ; i < n-; ++ i){
  9. if(i != && num[i]== num[i-]) continue;
  10. for(int j = i+; j < n-; ++ j){
  11. if(j!=i+ && num[j] == num[j-] ) continue;
  12. int start = j+, end = n-;
  13. while(start < end){
  14. int sum = num[i]+num[j]+num[start]+num[end];
  15. if(sum > target) end--;
  16. else if(sum < target) start++;
  17. else{
  18. vector<int> a;
  19. a.push_back(num[i]);a.push_back(num[j]);
  20. a.push_back(num[start]);a.push_back(num[end]);
  21. res.push_back(a);
  22. do{start++;}while(start < end && num[start] == num[start-]);
  23. do{end--;}while(start < end && num[end] == num[end+]);
  24. }
  25. }
  26. }
  27. }
  28. return res;
  29. }
  30. };
  1.  
  1.  

Leetcode 4Sum的更多相关文章

  1. LeetCode——4Sum &amp; 总结

    LeetCode--4Sum & 总结 前言 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxia ...

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

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

  3. [LeetCode] 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 ...

  4. leetcode — 4sum

    import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * Source : https://oj.l ...

  5. LeetCode 4Sum 4个数之和

    题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...

  6. Leetcode: 4Sum II

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

  7. leetcode 4sum python

    class Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :t ...

  8. LeetCode 4Sum (Two pointers)

    题意 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——4Sum

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

随机推荐

  1. 海思h264解码库

    海思的dll,解码h264  解码后转出yuv12 dll自己百度下载  hi_h264dec.dll   hi_h264dec_w.dll   调用方法: if (H264Dec.Hi264DecA ...

  2. Android 签名证书

    Android APK的数字签名的作用和意义 http://blog.csdn.net/gaomatrix/article/details/6568191 http://jingyan.baidu.c ...

  3. Backbone 学习笔记

    Backbone 是一款基于模型-视图-控制器 MVC 模式的轻量级javascript 框架 ,可以用来帮助开发人员创建单页Web应用. 借助Backbone 我们可以使用REST的方式来最小化客户 ...

  4. 跟随 Web 标准探究DOM -- Node 与 Element 的遍历

    写在前面 这篇没有什么 WebKit 代码的分析,因为……没啥好分析的,在实现里无非就是树的(先序DFS)遍历而已,囧哈哈哈……在WebCore/dom/Node.h , WebCore/dom/Co ...

  5. JavaScript常用技术总结!~~

    //如果当前窗口不是最外层窗口,把最外层窗口链接改成当前窗口 if (window != top) top.location.href = location.href; //value值移入消失 $( ...

  6. golang笔记——array

    1.定义一个 array 数组长度也是类型的一部分,比如长度为3的int数组与长度为5的int数组,并不是同一类型. package main import ( "strconv" ...

  7. Linux启动过程详解(inittab、rc.sysinit、rcX.d、rc.local)

    启动第一步--加载BIOS 当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬 ...

  8. java笔记--关于线程同步(7种同步方式)

    关于线程同步(7种方式) --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3897440.html"谢谢-- 为何要使用同步? ...

  9. jquery.SuperSlide.js只需要调用一个插件就能实现网页大部分特效--推荐

    很棒的一个插件,http://www.superslide2.com/

  10. java程序打包成jar

    1. 建立文件夹:proj,在该文件夹下建立3个子文件夹:lib,src 2. 在lib文件夹中放置依赖的jar包 3. 在src中放置类文件:com.cnjava.demo.Main.java 4. ...