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 unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
人家想法:
0. 先排序,再次注意C++排序的使用:sort(A.begin(), A.end());
- 固定1个值(用i指向),另两个值依据
sum=0 - A[i]的情况,通过两个指针lo, hi往中间扫;
具体的: A[lo] + A[hi] == sum时,lo++, hi--;A[lo] + A[hi] < sum时,说明和太小,那就向右移动 lo 指针;A[lo] + A[hi] > sum时,说明和太大,那就向左移动 hi 指针;- 消除
i, lo, hi指针处的重复值 是另一个难点,注意观察下面程序咋做的.
自个代码及注释:
\(O(n^2)\) time, \(O(1)\) space.
vector<vector<int>> threeSum(vector<int>& A) {
int n = A.size();
sort(A.begin(), A.end());
vector<vector<int>> res;
for (int i = 0; i < n - 2; i++) {
if (A[i] > 0) break;
// 消除i处重复值
if (i == 0 || (i > 0 && A[i] != A[i - 1])) {
int lo = i + 1, hi = n - 1, sum = 0 - A[i];
while (lo < hi) {
if (A[lo] + A[hi] == sum) {
// 精华之处
// 若相等,则移动lo, hi,不可只移动lo或hi,因为这是增序
vector<int> triplet(3, 0);
triplet[0] = A[i];
triplet[1] = A[lo];
triplet[2] = A[hi];
res.push_back(triplet);
//消除lo,hi处重复值
while (lo < hi && (A[lo] == A[lo + 1])) lo++;
while (lo < hi && (A[hi] == A[hi - 1])) hi--;
lo++; hi--;
}
//此处无需消除重复值,大不了lo,hi之和仍小于sum,继续移动就是了
else if (A[lo] + A[hi] < sum) lo++;
else hi--;
}
}
}
return res;
}
15. 3Sum(中等)的更多相关文章
- 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 ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- 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 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 15. 3Sum C++
参考资料: https://leetcode.com/problems/3sum/discuss/7402/Share-my-AC-C%2B%2B-solution-around-50ms-O(N*N ...
随机推荐
- api-gateway实践(09)支持rest服务注册
一.GET-GET 1.前端定义 2.后端定义 2.1.基础定义 2.2.path参数.head参数.query参数 2.3.常量参数 2.4.系统参数 2.5.结果定义 二.POST-POST 1. ...
- 新概念英语(1-1)Excuse me!
Excuse me!Whose handbag is it? A:Excuse me! B:Yes? A:Is this your handbag? B:Pardon? A:Is this your ...
- Linux搭建Apache+Tomcat实现负载均衡
一.首先需要安装java,详见http://www.cnblogs.com/fun0623/p/4350004.html 二.编译安装Apache,详见http://www.cnblogs.com/f ...
- Python学习之list有序集合
# coding=utf-8 # list有序集合 classmate = ['Michael', 'Bob', 'Tracy'] print classmate print len(classmat ...
- 将Excel上千条数据写入到数据库中
简要说明:因工作需要,需要一张Excel表格中的所有数据导入到数据库中.如下表,当然这只是一部分,一共一千多条. 前期处理: 首先要保证上图中的Excel表格中的数据不能为空,如果有为空的数据,可以稍 ...
- Vue框架之双向绑定事件
Vue框架之双向绑定事件 首先介绍下Vue框架的语法 vue通过 {{temp}} 来渲染变量 {{count+100}} # 求和 v-text # 为标签插入text文本 v-html # 为标签 ...
- AtCoder Beginner Contest 073
D - joisino's travel Time Limit: 2 sec / Memory Limit: 256 MB Score : 400400 points Problem Statemen ...
- Docker:云栖社区开源论题及Spark开源论题
https://yq.aliyun.com/topic/78?spm=5176.8290451.656547.7.rMYhAF https://yq.aliyun.com/activity/155?u ...
- JavaScript的屏幕对象
screen 屏幕对象 反映了当前用户的屏幕设置. width 返回屏幕的宽度(像素数). height 返回屏幕的高度. availWidth 返回屏幕的可用宽度(除去了一些不自动隐藏的类似任务栏的 ...
- 关于OpenAuth.Net被攻击的感想
距离上次写博客应该是1年多以前的事情了,看过我博客的人都知道,我从来不在博客园发技术无关的贴子,除了上次离职.但这次我是实在忍不住了. 今天我个人开源项目OpenAuth.Net发布了最新版(有兴趣戳 ...