#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. Android中为窗口定义主题

    在res/values/styles文件夹中定义如下: <style name="myTheme"> <item name="android:windo ...

  2. 插入排序之python实现源码

    def insert_sort(old): for i in range(1, len(old)): for j in range(i, 0, -1): if(old[j] < old[j-1] ...

  3. thinkphp 整合 ucenter

    http://xcodebox.com/2013/06/8855.html 参考 1   ucenter源码目录下 /advanced/examples/api目录 copy到thinkphp项目根目 ...

  4. Java基础巩固--正则表达式

    本篇文章是学习尚学堂的关于正则表达式的视频教程时,所做的笔记.供广大编程爱好者学习之用,也留给日后自己复习使用! 1.为什么要有正则表达式? 正则表达式可以方便的对数据进行匹配,可以进行更加复杂的字符 ...

  5. JavaScript符串中每个单词的首字母大写化

    map() + replace() function titleCase(str) { var convertToArray = str.toLowerCase().split(" &quo ...

  6. How to say all the keyboard symbols in English and Chinese

    How to say all the keyboard symbols in English Symbol English 中文 ~ tilde 波浪号 ` grave accent, backquo ...

  7. 响应式页面字体用什么单位:rem

    html:62.5%//10pxbody:1.4rem;//14px... <!doctype html> <html> <head> <title>a ...

  8. 一个小程序[Socrates]中学到的Perl点滴

    1. 抓取网页源文件,只要三行代码 use LWP::Simple; $url='http://music.baidu.com/top/dayhot'; $page=get($url) or die ...

  9. delphi cxgrid导出excel去除货币符号

    版本 : devexpress 13.1.4 打开 包在ExpressExportLibary目录中.  修改FCells.SetCellDataCurrency为FCells.SetCellData ...

  10. 旧版Xcode下载地址

    怕忘记了,做个记号 https://developer.apple.com/downloads/