题意

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.

找出一个数组中的三个数,使这三个数的和为0。输出所有的组合,不能重复。

解法

最简单的思路就是跑一个三层循环,暴力枚举所有组合,很显然会超时。

然后考虑排序后跑两层循环,第三层改用二分查找,即确定前两个数后用二分来搜第三个数,时间复杂度降到了O(logN * N^2),还是会超时。

最后,采用了Two Sum这一题的办法,遍历第一个数,然后剩下的两个数用双指针算法来找,这样时间复杂度就降到了O(N^2)

还有一个问题是判重,这里采用的办法是将三个数拼接起来成为一个数,比如【-1,0,1】就被保存成-101,用Long Long来存,然后放到一个Map里,每次选取新答案时都判断一下这样的组合是不是能在Map里找到。

class Solution
{
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
map<long long,bool> vis;
sort(nums.begin(),nums.end()); vector<vector<int>> rt;
for(int i = 0;i < nums.size();i ++)
{
if(nums[i] > 0)
break;
int j = i + 1;
int k = nums.size() - 1;
while(j < k)
{
if(nums[i] + nums[j] + nums[k] == 0)
{
long long box = abs(nums[i]); // 判重
int temp = abs(nums[j]);
while(temp)
{
box *= 10;
temp /= 10;
}
box += abs(nums[j]);
temp = abs(nums[k]);
while(temp)
{
box *= 10;
temp /= 10;
}
box += abs(nums[k]);
if(nums[i] * nums[j] * nums[k] < 0)
box = -box; if(vis.find(box) == vis.end())
{
vis[box] = true;
rt.push_back({nums[i],nums[j],nums[k]});
}
j ++;
} if(nums[i] + nums[j] + nums[k] < 0)
j ++;
else if(nums[i] + nums[j] + nums[k] > 0)
k --;
}
}
return rt;
}
};

LeetCode 3Sum (Two pointers)的更多相关文章

  1. LeetCode 3Sum Closest (Two pointers)

    题意 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  2. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  3. [LeetCode] 3Sum Closest 最近三数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  4. [LeetCode] 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 ...

  5. LeetCode 3Sum Smaller

    原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...

  6. leetcode — 3sum

    import java.util.*; /** * Source : https://oj.leetcode.com/problems/3sum/ * * Created by lverpeng on ...

  7. LeetCode 4Sum (Two pointers)

    题意 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  8. LeetCode: 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 ...

  9. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

随机推荐

  1. C#读取AD域用户信息

    private const string domainName = "本机IP地址或域名"; private const string adAdmin = "管理员帐号& ...

  2. python Anaconda

    转载自   https://blog.csdn.net/program_developer/article/details/79677557 目录: Anaconda是什么? 如何安装? 如何管理包? ...

  3. python永久添加第三方模块,PYTHONPATH的设置

    今天用pip安装pymysql后遇到了一个问题,在PyCharm中import pymysql模块时,运行却提示我找不到pymysql mudule 我先考虑的是pymysql没有安装成功,但是cmd ...

  4. 俄罗斯方块(JS+CSS)

    这是一个用 js + css 写的网页版俄罗斯方块. 具体代码与示例可访问我的另一个博客查看,源码与示例.

  5. 【转】Java学习---集合框架那些事

    [原文]https://www.toutiao.com/i6593220692525711885/ Arraylist 与 LinkedList 异同 1. 是否保证线程安全: ArrayList 和 ...

  6. VS2017C++单元测试

    0.欢迎食用 希望对点进来的你有所帮助. 全文记流水账,内心想法如示例项目名称. 1.建立需测试的项目 新建项目 正常书写.h 和.cpp文件 2.新建单元测试 右击解决方案 -> 添加 -&g ...

  7. angularjs ngRoute和ui.router对比

    ngRoute模块是angularjs自带的路由模块,ui.router是一个第三方路由模块,接下来将对两者进行一个对比: ng-router(angular-router.js) ng-view n ...

  8. 关于Java集合类库中的几种常用队列

    Java中几种常用的队列 阻塞队列与普通队列的区别在于,当队列是空的时,从队列中获取元素的操作将会被阻塞,或者当队列是满时,往队列里添加元素的操作会被阻塞.试图从空的阻塞队列中获取元素的线程将会被阻塞 ...

  9. Rx编程的第一步是将native对象转换为monad对象

    Rx编程的第一步是将native对象转换为monad对象 将基础类型转换为高阶类型,以便使用函数式编程的特性.

  10. BZOJ3673/3674:可持久化并查集

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...