leetcode-algorithms-18 4Sum

Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

解法

解法同题15

class Solution
{
public:
vector<vector<int>> fourSum(vector<int>& nums, int target)
{
vector<vector<int>> r; sort(nums.begin(), nums.end());
int n = nums.size();
if (n < 4) return r;
for (int i = 0; i < n; ++i)
{
int target3 = target - nums[i];
if (target3 < 0 && nums[i] >= 0) break;
for (int j = i + 1; j < n; ++j)
{
int target2 = target3 - nums[j];
if (target2 < 0 && nums[j] >= 0) break; int front = j + 1;
int back = n - 1;
while(front < back)
{
int two = nums[front] + nums[back]; if (two > target2)
--back;
else if (two < target2)
++front;
else
{
vector<int> t(4,0);
t[0] = nums[i];
t[1] = nums[j];
t[2] = nums[front];
t[3] = nums[back];
r.push_back(t); ++front; --back;
while((front < back) && nums[front] ==t[2]) ++front;
while((front < back) && nums[back] == t[3]) --back;
}
} while (j < n && nums[j + 1] == nums[j]) ++j;
} while (i < n && nums[i + 1] == nums[i]) ++i;
}
return r;
}
};

链接: leetcode-algorithms 目录

leetcode-algorithms-18 4Sum的更多相关文章

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

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

  2. LeetCode:18. 4Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个 ...

  3. 【LeetCode】18. 4Sum 四数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...

  4. 【一天一道LeetCode】#18. 4Sum

    一天一道LeetCode (一)题目 Given an array S of n integers, are there elements a, b, c, and d in S such that ...

  5. 《LeetBook》leetcode题解(18) : 4Sum[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  6. 【LeetCode】18. 4Sum (2 solutions)

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

  7. 【LeetCode】18. 4Sum

    题目: 思路:这题和15题很像,外层再加一个循环稍作修改即可 public class Solution { public List<List<Integer>> fourSu ...

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

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

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

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

随机推荐

  1. 论文笔记之:DualGAN: Unsupervised Dual Learning for Image-to-Image Translation

    DualGAN: Unsupervised Dual Learning for Image-to-Image Translation 2017-06-12  21:29:06   引言部分: 本文提出 ...

  2. 案例1:写一个压缩字符串的方法,例如aaaabbcxxx,则输出a4b2c1x3。

    public static String zipString(String str){ String result = "";//用于拼接新串的变量 char last = str ...

  3. kylin3

    RDBMS: 关系数据库管理系统(Relational Database Management System),是将数据组织为相关的行和列的系统,而管理关系数据库的计算机软件就是关系数据库管理系统, ...

  4. 那些年我们一起用过的 PHP 函数(一)

    用这个帖子记录一下 PHP 中比较常用的函数以及使用过程中需要注意的点,此贴持续更新 1.strpos() 函数     首先,我们知道 strpos() 函数是用于查找一个字符串在另一个字符串第一次 ...

  5. jmeter命令行模式运行,实时获取压测结果

    jmeter命令行模式运行,实时获取压测结果 jmeter很小,很快,使用方便,可以在界面运行,可以命令行运行.简单介绍下命令行运行的方式: sh jmeter.sh -n -t my-script. ...

  6. android获取屏幕宽度和高度

    1. WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE); int wi ...

  7. 蚂蚁金服 Service Mesh 渐进式迁移方案|Service Mesh Meetup 实录

    小蚂蚁说: 本文是基于在 Service Mesher Meetup 上海站的主题分享<蚂蚁金服 Service Mesh 渐进式迁移方案>内容整理,完整的分享 PPT 获取方式见文章底部 ...

  8. JS字典 Dictionary类

    字典 Dictionary类 /*字典 Dictionary类*/ function Dictionary() { this.add = add; this.datastore = new Array ...

  9. animate.css –齐全的CSS3动画库--- 学习笔记

    animate.css – 齐全的CSS3动画库 学习网站: https://daneden.github.io/animate.css/ http://www.dowebok.com/98.html ...

  10. CentOS6.5使用yum快速搭建LAMP环境

    1.安装Apache # yum -y install httpd # 开机自启动 # chkconfig httpd on # 启动httpd 服务 # service httpd start # ...