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 ...
随机推荐
- 修改了SpringBoot的主类名称后,gradle build报错的解决办法
Unable to find a single main class from the following candidates [*.*Application]
- Server.MapPath找不到命名空间,解决办法
最近在做微信公众号开发,在网上找了个例子实现获取Access_token的值,需要读取xml文件,结果就遇到这个问题
- [CodeForces10D]LCIS(最长公共上升子序列) - DP
Description 给定两个数列,求最长公共上升子序列,并输出其中一种方案. Input&Output Input 第一行一个整数n(0<n<=500),数列a的长度. 第二行 ...
- Python django实现简单的邮件系统发送邮件功能
Python django实现简单的邮件系统发送邮件功能 本文实例讲述了Python django实现简单的邮件系统发送邮件功能. django邮件系统 Django发送邮件官方中文文档 总结如下: ...
- vscode设置出错, 无法自动补全
问题: 之前设置的没问题, vscode重装后, 发现vscode里面的设置还在, 但敲代码却无法识别虚拟环境中的包了, 因此相关的内容也无法自动补全. 解决: 后来发现, 实际上设置没有出错, 但重 ...
- 原来你是这样的Promise
1. Promise简介 promise是异步编程的一种解决方案,它出现的初衷是为了解决回调地狱的问题. 打个比方,我需要: --(延迟1s)--> 输出1 --(延迟2s)--> 输出2 ...
- c++简单线程池实现
线程池,简单来说就是有一堆已经创建好的线程(最大数目一定),初始时他们都处于空闲状态,当有新的任务进来,从线程池中取出一个空闲的线程处理任务,然后当任务处理完成之后,该线程被重新放回到线程池中,供其他 ...
- IIS7 http自动跳转到https
1.下载安装URL重写模块:Microsoft URL Rewrite Module 32位:http://download.microsoft.com/download/4/9/C/49CD28DB ...
- JavaScript树(一) 简介
树的相关术语 一个树结构包含一系列存在父子关系的节点. 每个节点都有一个父节点 (除了顶部的第一个节点)以及零个或多个子节点: 位于树顶部的节点叫作根节点(11) .它没有父节点.树中的每个元素都叫作 ...
- java代码优化细节
在代码线上运行的过程中,往往会出现很多我们意想不到的错误,不少错误定位到最后往往是一个非常小的原因导致的.然而因为线上环境和开发环境是非常不同的,为了解决一个错误,我们需要先查找错误原因.修改验证.打 ...