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. flask开发restful api

    flask开发restful api 如果有几个原因可以让你爱上flask这个极其灵活的库,我想蓝图绝对应该算上一个,部署蓝图以后,你会发现整个程序结构非常清晰,模块之间相互不影响.蓝图对restfu ...

  2. golang做的邮件服务器

    https://gowalker.org/github.com/gleez/smtpd https://www.v2ex.com/t/133221

  3. vc 制作图片资源dll

    方法一: 使用纯WIN32 DLL方法封装纯资源第一步,通过VS2005建立WIN32 DLL 空工程第二步,设置配置属性->链接器->高级->无入口点(是/NOENTRY)设置配置 ...

  4. Java线程并发中常见的锁--自旋锁 偏向锁

    随着互联网的蓬勃发展,越来越多的互联网企业面临着用户量膨胀而带来的并发安全问题.本文着重介绍了在java并发中常见的几种锁机制. 1.偏向锁 偏向锁是JDK1.6提出来的一种锁优化的机制.其核心的思想 ...

  5. 第十七篇:实例分析(1)--初探WDDM驱动学习笔记(八)

    第四篇(VidPN)中提到过MIRROR驱动. 在进入本篇的实际内容前, 带着好奇心, 想请教CSDN中的显卡驱动方面的大虾, 怎样才干把这个驱动玩起来, 这个驱动的作用是什么,等等, 敬请不吝赐教. ...

  6. linux route命令的使用详解

    route命令用于显示和操作IP路由表.要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现.在Linux系统中,设置路由通常是 为了解决以下问题:该Linu ...

  7. [Android]Volley源代码分析(店)应用

    通过前面的谈话,我相信你有Volley有了一定的了解了原理.本章将给出一些我们的应用程序都可以在样品中直接使用,第一样品是 NetworkImageView类,事实上NetworkImageView顾 ...

  8. 【剑指offer】和为定值的两个数

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/24933341 题目描写叙述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的 ...

  9. 分类器是如何做检测的?——CascadeClassifier中的detectMultiScale函数解读

    原地址:http://blog.csdn.net/delltdk/article/details/9186875 在进入detectMultiScal函数之前,首先需要对CascadeClassifi ...

  10. 从零开始,使用python快速开发web站点(2)

    书接上文.http://blog.csdn.net/i7788/article/details/10306595 首先是数据库的搭建,这里的django的数据模型搭建十分easy. no sql.ju ...