#include <string>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

vector<vector<int>> fourSum(vector<int>& nums, int target) {
    vector<vector<int>> res;
    sort(nums.begin(), nums.end());
    int len = nums.size();
   
    for (int i = 0; i < len; i++) {
        for (int j = i + 1; j < len; j++) {
            int third = j + 1, fouth = len - 1;
            while (third < fouth) {
                int sum = nums[i]+nums[j]+ nums[third]+nums[fouth];
                if (target == sum) {
                    vector<int> temp = { nums[i],nums[j],nums[third],nums[fouth] };
                    res.push_back(temp);
                    while (third < fouth && nums[third ] == temp[2]) third++;
                    while (third < fouth && nums[fouth ] == temp[3]) fouth--;
                }
                else sum < target ? third++ : fouth--;
            }
            while ((j + 1 < len) && nums[j] == nums[j + 1]) j++;
        }
        while ((i + 1 < len) && nums[i] == nums[i + 1]) i++;
    }
    return res;
}
int main() {
    vector<int> nums = { 1,0,-1,0,-2,2};
    int target = 0;
    vector<vector<int>> res=fourSum(nums, target);
}

18. 4Sum的更多相关文章

  1. [LeetCode][Python]18: 4Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...

  2. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  3. 1. Two Sum&&15. 3Sum&&18. 4Sum

    题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...

  4. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  5. 15. 3Sum、16. 3Sum Closest和18. 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

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

  8. 18. 4Sum (JAVA)

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  9. LeetCode——18. 4Sum

    一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...

  10. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

随机推荐

  1. IIS配置及防黑

    安装IIS.部署网站(发布或者拷贝都可以).修改连接字符串,compilation设为false,删掉cs代码 上传文件夹不给执行权限: 在iis管理器中找到上传文件夹,选择属性--执行权限,设置为“ ...

  2. Python3 列表

    list是一种有序的集合,可以随时添加和删除其中的元素. >>> classmates = ['Michael', 'Bob', 'Tracy'] >>> clas ...

  3. Visual Studio2012中搭建WCF项目

    分布式系统:指在系统与系统之间进行通信,系统不再是孤立的,例如:淘宝查看物流信息,或是hao123的天气预报,这些可能都是用的别的系统的web方法. 1.创建空的解决方案 2.新建项目-WCF服务库项 ...

  4. CentOS 6.4 播放avi格式的视频文件

    1. 需要先进行相关的yum源的导入: rpm -Uhv http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/rpmforge-release-0. ...

  5. 64位AutoItLibrary的安装

    安装AutoItLibrary,除了要先已经安装好Robotframework之外,先要安装一个叫pywin32的工具 第一步:pywin32的安装 pywin32的下载地址: http://sour ...

  6. UIPageControll - 图片格式

    设置pageCon的显示风格: 1. 颜色 page.pageIndicatorTintColor = [UIColor redColor]; page.currentPageIndicatorTin ...

  7. 我的pch文件

    /** *  1. RGB背景色 */ #define PPCOLOR_RGB(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue: ...

  8. Cocoa Touch 框架

    Cocoa Touch包括: (1)Core Animation: 通过Core Animation,可以通过一个基于独立图层的简单的编程模型来创建丰富的用户体验. (2)Core Audio: Co ...

  9. Spring 3整合Quartz 2实现定时任务一:常规整合 (基于maven构建)

    最近工作中需要用到定时任务的功能,虽然Spring3也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之后,决定整合更为专业的Quartz来实现定时任务功能. 首先,当然是添加依 ...

  10. Delphi XML-RPC 中文乱码解决方法

    http://download.csdn.net/user/csm2432/uploads/2