15.3Sum (Two-Pointers)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- The solution set must not contain duplicate triplets.
思路:首先大循环第一个数,从第一个数之后首尾指针向中间夹逼,时间复杂度O(n2)。需要注意跳过重复的数字。
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int size = nums.size();
if(size < ) return result;
sort(nums.begin(), nums.end());
find(nums, , size-, -nums[]);
for(int i = ; i < size-; i++){
if(nums[i]!=nums[i-]) find(nums, i+, size-, -nums[i]);
}
return result;
}
void find(vector<int>& nums, int start, int end, int target){
int sum;
while(start<end){
sum = nums[start]+nums[end];
if(sum == target){
item.clear();
item.push_back(-target);
item.push_back(nums[start]);
item.push_back(nums[end]);
result.push_back(item);
do{
start++;
}while(start!= end && nums[start] == nums[start-]);
do{
end--;
}while(end!=start && nums[end] == nums[end+]);
}
else if(sum>target){
do{
end--;
}while(end!=start && nums[end] == nums[end+]);
}
else{
do{
start++;
}while(start!= end && nums[start] == nums[start-]);
}
}
}
private:
vector<vector<int>> result;
vector<int> item;
};
15.3Sum (Two-Pointers)的更多相关文章
- *15. 3Sum (three pointers to two pointers), hashset
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- 1. Two Sum&&15. 3Sum&&18. 4Sum
题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- 15. 3Sum、16. 3Sum Closest和18. 4Sum
15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...
- 刷题15. 3Sum
一.题目说明 题目非常简洁15. 3Sum,读懂题目后,理解不难. 但 实话说,我们提交代码后,Time Limit Exceeded,最主要的是给了非常长的测试用例,我本地运行后87秒,确实时间非常 ...
- [LeetCode] 15. 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode 15. 3Sum(三数之和)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
随机推荐
- js设计模式整理
单例模式 恶汉式单例 实例化时 懒汉式单例 调用时构造函数模式 1.实现一 function Car(model, year, miles) { this.model = model; this.ye ...
- vim没有权限却可以强制保存时所引起的思考 ------ 文件夹权限对所属文件的权限影响
最近在拿着Linux 鸟叔私房菜对着Linux 系统学习一下基本操作,虽然已经使用Linux系统已经好多年不过却一直没有系统的学习一下.在用vim 编辑一个文件的时候出现了一个很神奇的事情,明明该文件 ...
- ambassador 学习九 多ambassador部署说明
目前官方稳文档没有写,但是demo 里面有,所以就整理出来,其实目前demo里面的 多实例部署用了多个服务的service(使用nodeport 暴露地址,具体使用就是制定ambassador 实例的 ...
- Android开发入门
教我徒弟Android开发入门(一) 教我徒弟Android开发入门(二) 教我徒弟Android开发入门(三) 出处:http://www.cnblogs.com/kexing/tag/Androi ...
- C#多线程应用:子线程更新主窗体控件的值(一)
我记得以前写过一次关于多线程的调用及更新的文章,由于时间比较久了,现在一时没找到.在做项目的时候,用到了多线程,还是有很多的同事在问多线程更新主窗体的事情,现在就这个事情做个记录. 说起多线程之间的更 ...
- Chrome 解决flash问题
Chrome 无法显示使用插件的内容 Chrome 不再支持很多插件.不过网站创建者已经通过更安全的方式,将多数这类功能添加到 Chrome 中. 为什么 NPAPI 插件现在无法正常运行过去,许多插 ...
- 02 - Unit010:关联映射
关联映射 什么是? 数据库中有关联关系的表,通过实体对象引用的方式体现出来,叫关联映射. 为什么? 将多表的记录封装成实体对象. 何时用? 对数据库中的表进行多表查询时. 怎么用? cn_user-- ...
- XSS 与 CSRF 两种跨站攻击
在前几年,大家一般用拼接字符串的方式来构造动态 SQL 语句创建应用,于是 SQL 注入成了很流行的攻击方式, 但是现在参数化查询 已经成了普遍用法,我们已经离 SQL 注入很远了.但是历史同样悠久的 ...
- 黄聪:VS2010中如何让webbrowser不弹出JS异常错误窗口(c#.net)
1.在属性窗口找到ScriptErrorsSuppressed,选择"true",这个选择的意思是,如果网页上有出现错误命令,这个错误提示将被抑制 2.[项目管理那里,在项目上右击 ...
- 关于android 动态设置view 样式 问题(默认style)
解决方案一: Button btn=new Button(new ContextThemeWrapper(mContext,R.style.service_text_dialog_style),nul ...