Given an array S of n integers, are there elements a, b, c,
and d in S such that a + b + c + d = target? Find all
unique quadruplets in the array which gives the sum of target. Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {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)

这是求四个数的和的问题。能够和前面三个数和的问题一样转化成两个数和的问题。即先固定第一个数。再依据第一个数固定第二个数,然后就是求两数和的问题了。数组里面的元素可能会有反复元素,所以须要注意消除反复的推断。时间复杂度是O(N^3)。

runtime:192ms

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result;
if(nums.size()<4)
return result; sort(nums.begin(),nums.end());
vector<int>::iterator iter1=nums.begin();
vector<int>::iterator iter2=iter1+1; //最外层第一个指针遍历
for(;iter1!=nums.end()-3;iter1++)
{ //固定第一个指针后第二个指针遍历
for(iter2=iter1+1;iter2!=nums.end()-2;iter2++)
{ //内层两个指针从两个方向遍历,这是经典的求两个数的和的问题
int base=*iter1+*iter2;
vector<int>::iterator iter3=iter2+1;
vector<int>::iterator iter4=nums.end()-1;
while(iter3<iter4)
{
if((*iter3+*iter4+base)<target)
{
iter3++;
}
else if((*iter3+*iter4+base)>target)
{
iter4--;
}
else
{
//这里须要注意vector中push_back和用下标訪问的差别,用小标訪问时相似于数组,所以tmp初始化时须要指定vector的大小,可是用push_back时不要指定大小。否则小心push_back是在你指定大小的vector后加入元素
vector<int> tmp;
tmp.push_back(*iter1);
tmp.push_back(*iter2);
tmp.push_back(*iter3);
tmp.push_back(*iter4);
result.push_back(tmp); //这是推断是否有反复的方式,假设下一指针依旧小于iter4而且下一个指针指向的值与当前值同样。iter3须要加1
while((iter3+1)<iter4 && *(iter3)==*(iter3+1)) iter3++; while(iter3<(iter4-1)&& *(iter4)==*(iter4-1)) iter4--; //推断完毕后还须要将iter3加1或iter4减一以继续查寻下一个和为常数的组合
iter3++;
}
} //对于iter2也须要推断是否存在反复元素问题
while(((iter2+1)!=(nums.end()-2))&&(*(iter2)==*(iter2+1))) iter2++; } //同理对于ier1也是如此
while(((iter1+1)!=(nums.end()-3))&&(*(iter1)==*(iter1+1))) iter1++; }
return result;
}
};

LeetCode18:4Sum的更多相关文章

  1. No.018:4Sum

    问题: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  2. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  3. LeetCode OJ:4Sum(4数字之和)

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

  4. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  5. LeetCode——18. 4Sum

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

  6. [LeetCode] 454. 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  7. LeetCode 018 4Sum

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

  8. [LeetCode] Two Sum 两数之和

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  9. Leetcode OJ 刷题

    Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...

随机推荐

  1. MySQL 执行计划里的rows

    <pre name="code" class="html">SQL> alter session set statistics_level=a ...

  2. POJ1087 A Plug for UNIX 【最大流】

    A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13855   Accepted: 4635 ...

  3. Photoshop图象切片保存为网页HTML(DIV+CSS布局)的方法

    首先,制作图象切片(以一张图片为例子) 一.选择“切片”工具,在图像上拖动以分割图像(例如:一张图像切割2次就形成3个切片)切片后如下图 二.设置切片选项(如大小.目标链接.图片说明等等):选择“切片 ...

  4. 忽然想到:把Mu的源代码一网打尽

    那么那些流媒体开发的公司,就不会拒绝我了,真是一举两得.

  5. BestCoder Round #3HDU 4907

    1. HDU 4907:http://acm.hdu.edu.cn/showproblem.php?pid=4907 中文题我就不说题意了,直接说解题思路吧! ① 第一种思路就是我比赛时的思路,将a数 ...

  6. Cocos2d-x-lua游戏两个场景互相切换MainScene01切换到MainScene02

    /* 场景一lua代码 */ require "MainScene02" local dic_size = CCDirector:sharedDirector():getWinSi ...

  7. hdu1695(莫比乌斯反演)

    传送门:GCD 题意:求[1,n],[1,m]gcd为k的对数. 分析:莫比乌斯入反演门题,gcd(x,y)==k等价于gcd(x/k,y/k)==1,求出[1,n][1,m]互质的对数,在减去[1, ...

  8. 启动和关闭JBoss As 7.1.1脚本

    启动和关闭JBoss As 7.1.1,脚本例如以下djboss.sh: #!/bin/sh #JBOSS_HOME JBOSS_HOME=/opt/jboss case "$1" ...

  9. fastdfs storage server的设计与实现

     fastdfs是一个针对互联网应用设计的分布式文件系统.具有架构简单.结构清晰.代码量小等特点. 详细的介绍及架构请參考分布式文件系统FastDFS架构剖析(http://www.program ...

  10. umlの实现图

    在uml中大部分模型描写叙述了逻辑和设计方面的信息: 用例图知道期望 类图能够知道问题域的词汇(类.对象) 状态图.交互图和活动图能够知道类图中的词汇是怎样写作完毕行为的(逻辑结构) 实现图是用来描写 ...