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. 固定1个值(用i指向),另两个值依据 sum=0 - A[i] 的情况,通过两个指针lo, hi 往中间扫;

    具体的:
  2. A[lo] + A[hi] == sum时,lo++, hi--;
  3. A[lo] + A[hi] < sum时,说明和太小,那就向右移动 lo 指针;
  4. A[lo] + A[hi] > sum时,说明和太大,那就向左移动 hi 指针;
  5. 消除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(中等)的更多相关文章

  1. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  2. 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 ...

  3. 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 ...

  4. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  5. 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 = ...

  6. 刷题15. 3Sum

    一.题目说明 题目非常简洁15. 3Sum,读懂题目后,理解不难. 但 实话说,我们提交代码后,Time Limit Exceeded,最主要的是给了非常长的测试用例,我本地运行后87秒,确实时间非常 ...

  7. [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 ...

  8. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  9. 15. 3Sum C++

    参考资料: https://leetcode.com/problems/3sum/discuss/7402/Share-my-AC-C%2B%2B-solution-around-50ms-O(N*N ...

随机推荐

  1. apache修改最大连接数报错

    报错的内容: AH00180: WARNING: MaxRequestWorkers of 2500 exceeds ServerLimit value of 256 servers, decreas ...

  2. 使用net.sf.cssbox实现网页截图

    需要引用包,在pom.xml中添加引用: <dependency> <groupId>net.sf.cssbox</groupId> <artifactId& ...

  3. PIL绘图

    # coding:utf-8 # PIL的ImageDraw 提供了一系列绘图方法,让我们可以直接绘图.比如要生成字母验证码图片 from PIL import Image, ImageDraw, I ...

  4. logging格式

    import logging def foo(s): return 10 / int(s) def bar(s): return foo(s) * 2 def main(): try: bar(0) ...

  5. 基于 MySQL 的数据库实践(自然连接)

    在基本查询一节的示例中,我们有从 instructor 和 teaches 表组合信息,匹配条件是 instructor.ID 等于 teaches.ID 的查询,ID 属性是两个表中具有相同名称的所 ...

  6. 基于gin框架和jwt-go中间件实现小程序用户登陆和token验证

    本文核心内容是利用jwt-go中间件来开发golang webapi用户登陆模块的token下发和验证,小程序登陆功能只是一个切入点,这套逻辑同样适用于其他客户端的登陆处理. 小程序登陆逻辑 小程序的 ...

  7. 老刘 Yii2 源码学习笔记之 Component 类

    类图关系 属性与方法 class Component extends BaseObject { private $_events = []; private $_eventWildcards = [] ...

  8. 前端面试送命题-JS三座大山

    前言 本篇文章比较适合3年以上的前端工作者,JS三座大山分别指:原型与原型链,作用域及闭包,异步和单线程. 原型与原型链 说到原型,就不得不提一下构造函数,首先我们看下面一个简单的例子: functi ...

  9. jQuery中的for循环var与let的区别

    今天在写jQuery请求接口中发现一个问题: 在用AJAX发送请求中又嵌套了一个AJAX请求,发现在内层请求的success中对第一次success中的循环变量 i 无法获取,具体代码如下: $.aj ...

  10. [LeetCode] Valid Palindrome II 验证回文字符串之二

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...