LeetCode18:4Sum
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的更多相关文章
- 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 = ...
- 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 + ...
- 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 ...
- [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 ...
- LeetCode——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...
- [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 ...
- 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 ...
- [LeetCode] Two Sum 两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- Leetcode OJ 刷题
Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...
随机推荐
- 福州大学第十届校赛 & fzu 2128最长子串
思路: 对于每个子串,求出 母串中 所有该子串 的 开始和结束位置,保存在 mark数组中,求完所有子串后,对mark数组按 结束位置排序,然后 用后一个的结束位置 减去 前一个的 开始 位置 再 减 ...
- Swift - 页控件(UIPageControl)的用法
使用页控件可以用来展示多个桌面.比如很多应用第一次登陆时,会在开始页面使用页控件来介绍功能,通过左右滑动来切换页. 通常我们使用UIPageControl和UIScrollView相互结合来实现多页切 ...
- Swift - 控制流/控制结构说明(if,switch,for,while)
1,if语句 1 2 3 4 5 if count >=3 { println("yes") }else{ println("no") } ...
- KMP(Knuth-Morris-Pratt)算法
一.朴素匹配算法 也就是暴力匹配算法.设匹配字符串的长度为n,模式串的长度为m,在最坏情况下,朴字符串匹配算法执行时间为O((n - m + 1)m). 假设m = n / 2, 那么该算法的复杂度就 ...
- 论javascript模块化的优缺
如今backbone.emberjs.spinejs.batmanjs 等MVC框架侵袭而来.CommonJS.AMD.NodeJS.RequireJS.SeaJS.curljs等模块化的JavaSc ...
- object-c编程tips-timer
object-c定时器 object-c定时器会自己主动retain当前的使用者,假设不注意调用invalidate,则非常easy引起循环引用导致内存泄露.以下的思路提供了一套还算可行的解决方式. ...
- 1.VMwareTools安装
1 选中虚拟机.右击.然后点击:安装Vmware-tool(最好是有网络的情况下安装) 2 将Vmware-tool的安装文件复制到暂时文件夹下,截图例如以下: 3 解压VMwareTools- ...
- C#同步SQL Server数据库中的数据--数据库同步工具[同步新数据]
C#同步SQL Server数据库中的数据 1. 先写个sql处理类: using System; using System.Collections.Generic; using System.Dat ...
- DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)
DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类) 一.DAO模式简介 DAO即Data Access Object,数据访问接口.数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据 ...
- Config File Settings Of EF——实体框架的配置文件设置
我亦MSDN 原文地址 http://msdn.microsoft.com/en-us/data/jj556606 Entity Framework allows a number of settin ...