1. 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.
  2.  
  3. Note:
  4. Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a b c d)
  5. The solution set must not contain duplicate quadruplets.
  6. For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
  7.  
  8. A solution set is:
  9. (-1, 0, 0, 1)
  10. (-2, -1, 1, 2)
  11. (-2, 0, 0, 2)

  无聊 3sum 的变形

  1. class Solution {
  2. public:
  3. vector<vector<int> > fourSum(vector<int> &num, int target) {
  4. // Note: The Solution object is instantiated only once and is reused by each test case.
  5. vector<vector<int>> res;
  6. int len = num.size();
  7. if(len < ) return res;
  8. sort(num.begin(),num.end());
  9. for(int i = ; i< len-;++i){
  10. while(i> && i< len- && num[i] == num[i-])++i;
  11. for(int j = i+; j< len-; ++j){
  12. while(j!=i+ && j< len-&&num[j] == num[j-])++j;
  13. int left = j+;
  14. int right = len-;
  15. while(left < right){
  16. int sum = num[i] + num[j] +num[left]+num[right];
  17. if(sum == target){
  18. vector<int> ans;
  19. ans.push_back(num[i]);
  20. ans.push_back(num[j]);
  21. ans.push_back(num[left]);
  22. ans.push_back(num[right]);
  23. res.push_back(ans);
  24. left++;
  25. while(left<right && num[left]==num[left-]) ++left;
  26. right--;
  27. while(left < right && num[right] == num[right+]) --right;
  28. }else if(sum <target){
  29. left++;
  30. while(left<right && num[left]==num[left-]) ++left;
  31. }else{
  32. right--;
  33. while(left < right && num[right] == num[right+]) --right;
  34. }
  35. }//while(left <right)
  36. }//for j
  37. }// for i
  38. return res;
  39. }
  40. };

LeetCode_ 4 sum的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

随机推荐

  1. Collections之sort、reverse

    在使用List集合时,通常情况下希望从集合中得到的对象是按照一定顺序排列的,但是List集合的默认排序方式为按照对象的插入顺序,可以通过java.util.Collections类的静态方法sort( ...

  2. gradle 集成到myeclipse

    新的项目用到gradle,所以学了下,地址:http://dist.springsource.com/release/TOOLS/gradle :help 下,安装好,重启即可,gradle作为mav ...

  3. 狗血phonegap备忘录[3.3]

    phonegap平台就是个狗血的坑,最近的一个项目技术因为上面选型失败,使用了phonegap,加上客户的要求是"像微信一样",真可谓历经坎坷. 基本上评估一个项目是否应该或者可以 ...

  4. lvchange的available參数

    available參数在man info help中均无此參数,事实上參数为:activate 写此此.值得用的人注意. available 參数实为:  -a, --activate [a|e|l] ...

  5. Android用户界面概览

    用户界面的概观           全部的Android应用程序的用户界面元素都是用View和ViewGroup对象构建的.View就是在手机屏幕上描绘一个能够与用户交互的一个对象.ViewGroup ...

  6. ubuntu开机黑屏

    以下皆在VM虚拟机下 (1)ctr+alt+f4 进入命令行 (2) sudo apt-get update sudo apt-get install xserver-xorg-lts-quantal ...

  7. Linux Kernel: buffers和cached的区别

    The page cache caches pages of files to optimize file I/O. The buffer cache caches disk blocks to op ...

  8. iOS 开发-单元测试

    前言 维基百科对单元测试的定义如下: 在计算机编程中,单元测试(英语:Unit Testing)又称为模块测试, 是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小可 ...

  9. Gson解析json数据(转)

    一. www.json.org这是JSON的官方网站. 首先,我,我们需要在code.google.com/p/google-gson/downloads/list下载JSON的jar包,解析后把gs ...

  10. 【教训】rm -fr ./* 教训

    昨晚犯了一个重大错误,运行了 rm -rf ./* 本来是要删除一个不重要的目录的,结果在它的父目录下运行了上面命令,结果...都没了... 幸好数据库文件没有被删掉,数据还在,网站程序被删掉了,不久 ...