题目描述

给出一个有n个元素的数组S,S中是否有元素a,b,c和d满足a+b+c+d=目标值?找出数组S中所有满足条件的四元组。
注意:
  1. 四元组(a、b、c、d)中的元素必须按非降序排列。(即a≤b≤c≤d)
  2. 解集中不能包含重复的四元组。
    例如:给出的数组 S = {1 0 -1 0 -2 2}, 目标值 = 0.↵↵    给出的解集应该是:↵    (-1,  0, 0, 1)↵    (-2, -1, 1, 2)↵    (-2,  0, 0, 2)

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)↵

class Solution {
public:
    vector<vector<int> > fourSum(vector<int> &num, int target) {
        vector <vector<int>> res;
        vector< int> temp (4,0);
        set<vector<int> > st;
        sort(num.begin(),num.end());
        int n=num.size();
        for (int i=0;i<n;i++){
            for (int j=i+1;j<n;j++){
                int left=j+1,right=n-1;
                while (left<right){
                    int sum=num[i]+num[j]+num[left]+num[right];
                    if (sum==target){
                        temp[0]=num[i];
                        temp[1]=num[j];
                        temp[2]=num[left];
                        temp[3]=num[right];
                        st.insert(temp);
                    }
                    if (sum<target)
                        left++;
                    else
                        right--;
                }
            }
        }
        set<vector<int>>::iterator it;
        for (it=st.begin();it !=st.end();it++)
            res.push_back(*it);
        return res;
    }
};

leetcode132:4sum的更多相关文章

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

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

  3. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  4. 2016/10/28 很久没更了 leetcode解题 3sum问题进阶版4sum

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

  5. No.018:4Sum

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

  6. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  7. 3Sum & 4Sum

    3 Sum Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0? Find a ...

  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. 2sum、3sum、4sum以及任意连续的数的和为sum、任意连续或者不连续的数的和为sum

    2sum 如果数组是无序的,先排序(n*logn),然后用两个指针i,j,各自指向数组的首尾两端,令i=0,j=n-1,然后i++,j--,逐次判断a[i]+a[j]?=sum,如果某一刻a[i]+a ...

随机推荐

  1. vue使用vueCropper裁剪功能,代码复制直接使用

    //先安装包 npm install vue-cropper --save-dev <template> <div id="merchantInformation" ...

  2. thinkphp5.1 阿里云短信接口

    1.首先声明,我个人是没有,accessKeyId    accessKeySecret   SignName     TemplateCode这些参数是需要自己去,阿里云注册,生成的. 我用的密钥( ...

  3. 多测师讲解接口测试 _windows中搭建环境cms_高级讲师肖sir

    eclipse集成开发环境 搭建开发环境需要安装的工具如下 jdk-8u60-windows-x64.exe        jdk eclipse.rar      集成开发框架 mysql-inst ...

  4. 如何设计一个牛逼的API接口

    在日常开发中,总会接触到各种接口.前后端数据传输接口,第三方业务平台接口.一个平台的前后端数据传输接口一般都会在内网环境下通信,而且会使用安全框架,所以安全性可以得到很好的保护.这篇文章重点讨论一下提 ...

  5. 【C语言/C++编程学习笔记】你的第一个Windows程序!高级操作~

    什么是windows编程?了解到Windows API 编程.Windows编程.Windows SDK 编程是一个概念.今天我们运用C语言来实现你的第一个真正的Windows程序. windows. ...

  6. linux-设置hostname

    设置linux root后面的名字   

  7. df du linux空间清理,查看文件大小

    df -h ,这个命令用于查看服务器空间,运行后的效果图如下: [root@localhost /]# df -h Filesystem Size Used Avail Use% Mounted on ...

  8. ▶ 0001 No application 'E:\www\go\blog' found in your GOPATH

    go mod 配置 beego 首先cmd bee new blog go mod init 然后复制到任意目录 bee run 就会报错, 要退出该目录,进入上级目录 bee run blog 才行

  9. go 爬取页面保存

    package main import ( "bufio" "fmt" "io/ioutil" "net/http" & ...

  10. centos8平台使用vmstat监控系统

    一,vmstat的用途和特点: vmstat 是一个常用的系统性能分析工具,主要用来分析系统的内存使用情况,也常用来分析 CPU 上下文切换和中断的次数. 相对于 iostat 来说,vmstat 可 ...