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. docker 安装redis

    https://blog.csdn.net/chenjianandiyi/article/details/78962709 运行镜像redis:  docker run --name redis1 - ...

  2. (zhuan) Attention in Neural Networks and How to Use It

    Adam Kosiorek About Attention in Neural Networks and How to Use It this blog comes from: http://akos ...

  3. 重写NLog

    接口ILogBase: public interface ILogBase { void Debug(string message); void Debug(string message, Excep ...

  4. 在 JSDOM v11 中使用jQuery

    在JSDOM v11中使用jQuery 从v10开始,jsdom提供了更灵活的API. https://swashata.me/blog/use-jquery-jsdom-v11/ const tes ...

  5. php的Allowed memory size of 134217728 bytes exhausted问题

    提示Allowed memory size of 134217728 bytes exhausted,出现这种错误的情况常见的有三种: 0:查询的数据量大. 1:数据量不大,但是php.ini配置的内 ...

  6. MySQL like用法

    MySQL LIKE 语法 LIKE运算符用于WHERE表达式中,以搜索匹配字段中的指定内容,语法如下: WHERE column LIKE pattern WHERE column NOT LIKE ...

  7. java根据URL获取网页编码

    由于很多原因,我们要获取网页的编码(多半是写批量抓取的脚本吧...嘻嘻嘻) 注意: 如果你的目的是获取不乱码的网页内容(而不是根据网址发送post请求获取返回值),切记切记,移步这里 java根据UR ...

  8. 异步加载script,提高前端性能(defer和async属性的区别)

    一.异步加载script的好处 为了加快首屏响应速度,前端会采用代码切割.按需加载等方式优化性能.异步加载script也是一种前端优化的手段. 就好比如果我的页面其中一个功能需要打开地图,但是地图的j ...

  9. P1031 均分纸牌

    题目描述 有N堆纸牌,编号分别为 1,2,…,N1,2,…,N.每堆上有若干张,但纸牌总数必为N的倍数.可以在任一堆上取若干张纸牌,然后移动. 移牌规则为:在编号为1堆上取的纸牌,只能移到编号为2的堆 ...

  10. eclipse里安装SVN插件的两种方式

    eclipse里安装SVN插件,一般来说,有两种方式: 直接下载SVN插件,将其解压到eclipse的对应目录里 使用eclipse 里Help菜单的“Install New Software”,通过 ...