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 ...
随机推荐
- 《Java程序设计》十四次作业
<Java程序设计>十四次作业实验总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 3. 代码量统计 周次 总代码量 新增代码量 总文件数 新增 ...
- Beta阶段第2周/共2周 Scrum立会报告+燃尽图 14
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411] 版本控制:https://git.coding.net/liuyy08 ...
- jQuery progression 表单进度
progression.js是一款表单输入完成进度插件.支持自定义提示框大小.方向.左边.动画效果.间距等,也支持是否显示进度条.字体大小.颜色.背景色等. 在线实例 实例演示 使用方法 <fo ...
- 第8课 goto和void分析
遭人遗弃的goto: C语言是一种面向过程的结构化语言,其中主要结构有三种,顺序执行.选择执行.循环执行.再复杂的程序也是由这三种结构组合而成的. goto破坏了结构化特性,使程序以第四种方式执行,结 ...
- 我的AOP那点事儿--2
在<我的AOP那点事儿-1>中,从写死代码,到使用代理:从编程式AOP到声明式AOP.一切都朝着简单实用主义的方向在发展.沿着 Spring AOP 的方向,Rod Johnson(老罗) ...
- 02 - Unit06:弹出对话框
弹出对话框 如何实现弹出 //弹出出对话框 $("#can").load("alert/alert_notebook.html"); //显示背景色 $(&qu ...
- maven下载源代码,中文注释乱码的处理方法
通过maven下载的源码,如果有注释是中文的话,一般会有乱码问题,解决办法如下: Window -> Preferences -> Content Types -> Text -&g ...
- make 写法练习
cc=g++ all:signal %:%.o $(cc) -o $< $@ %.cpp:%.o echo se $< $@ $* $^ g++ -c $< $@cl: rm -rf ...
- Bootstrap-Plugin:附加导航(Affix)插件
ylbtech-Bootstrap-Plugin:附加导航(Affix)插件 1.返回顶部 1. Bootstrap 附加导航(Affix)插件 附加导航(Affix)插件允许某个 <div&g ...
- eclipse+maven springMVC搭建
1.新建项目: 选择Maven Project 选择项目位置,这里我选择的是C:\Users\admin\workspace\practice 选择maven项目类型,这里选择webapp: 填写Gr ...