#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. 九度OJ 1434 今年暑假不AC

    题目地址:http://ac.jobdu.com/problem.php?pid=1434 题目描述: “今年暑假不AC?”“是的.”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*% ...

  2. ActiveMQ基本配置与示例演示

    一.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 二.运行ActiveMQ 将apache-activemq-5.11.1-bin.zip解压,由于本系 ...

  3. js清空前后空格

    function trim(sValue){                var lastValue=this.replace(/(^\s*)|(\s*$)/g,"");     ...

  4. overflow:hidden真的失效了吗

    项目中常常有同学遇到这样的问题,现象是给元素设置了overflow:hidden,但超出容器的部分并没有被隐藏,难道是设置的hidden失效了吗? 其实看似不合理的现象背后都会有其合理的解释. 我们知 ...

  5. php操作memcache的用法、详解和方法介绍

    1.简介 memcache模块是一个高效的守护进程,提供用于内存缓存的过程式程序和面向对象的方便的接口,特别是对于设计动态web程序时减少对数据库的访问. memcache也提供用于通信对话(sess ...

  6. IOS 多线程之GCD

    参考:http://www.cnblogs.com/wendingding/p/3806821.html <<Objective-C基础教程>> 第二版 一 简介 GCD 全称 ...

  7. Visual C++ 6.0常用快捷键

    一.常用编译相关的快捷键 1.编译(单个文件)  Ctrl+F7 2.连接 F7 3.运行  Ctrl+F5 二.常用调试相关的快捷键 1.GO(全速运行)  F5 2.Stop Debuging(停 ...

  8. 警惕arm-linux-gcc编译器优化选项

    arm-linux-gcc的优化选项例如(-O2),可以加速我们的程序,使程序执行效率更高.但是,倘若我们就是需要程序慢一点运行,但是优化却把我们的延时函数优化的没有了的时候,这种优化却不是我们想要的 ...

  9. slivelight5和数据库交互

    最近开始研究sliverlight和数据库交互了,无奈网上资料较少,查阅了大量资料终于成功了,但是我记得还有别的方法,希望大家讨论一下 数据访问层我的用的是ado.net实体数据模型 然后新建了一个w ...

  10. designated initializer和secondary initializer是什么?

    仅在此简单记录概念,方便以后回顾... ===================================== designated initializer是指定初始化方法,提供所有参数: sec ...