题意

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. 大话存储 1 - 走进计算机IO世界

    组成计算机的三大件:CPU,内存和IO. 1 总线 总线就是一条或者多条物理上的导线,每个部件都接到这些导线上,同一时刻只能有一个部件在接收或者发送. 仲裁总线:所有部件按照另一条总线,也就是仲裁总线 ...

  2. tail 尾巴

    tail用法:尾巴,取文件的最后N行,默认前10行, -n 2 取前2行-n 2,简写就是-2 -f 文件 跟踪一个文件尾部的时时变化. 克隆出一个窗口执行:循环脚本:for n in `seq 1 ...

  3. Windows进程间的通信

    一.进程与进程通信   进程间通信(Interprocess Communication, IPC)是指不同的进程之间进行数据共享和数据交换. 二.进程间通信方式 1.  文件映射 注:文件映射是在多 ...

  4. elasticsearch之JAVA环境变量报错:could not find java; set JAVA_HOME or ensure java is in PATH

    在以RPM包安装elasticsearch过程中出现报错JAVA环境的问题: ● elasticsearch.service - Elasticsearch Loaded: loaded (/usr/ ...

  5. python 爬取全量百度POI

    在网上找了很多关于爬取百度POI的文章,但是对“全量”的做法并没有得到最终的解决方案,自己写了一个,但还是不能实现全量POI抓取,能够达到至少50%的信息抓取.注意:这里所指“全量”是能够达到100% ...

  6. 为什么说swift是面向协议编程--草稿

    为什么说swift是面向协议编程 public protocol ReactiveCompatible { /// Extended type associatedtype CompatibleTyp ...

  7. splay专题复习——bzoj 3224 & 1862 & 1503 题解

    [前言]快要省选二试了.上次去被虐出翔了~~这次即便是打酱油.也要打出风採! 于是暂停新东西的学习,然后開始复习曾经的知识.为骗分做准备.PS:区间翻转的临时跳过,就算学了也来不及巩固了. [BZOJ ...

  8. 计数dp

    计数dp 计数类的$dp$没做过几个,所以之前都放到"思维"标签下了,后来发现原来这属于一类问题啊...搬过来了. 管道取珠:https://www.lydsy.com/Judge ...

  9. text/html & text/plain的区别

    需要了解的概念 Content-Type:用于定义用户的浏览器或相关设备如何显示将要加载的数据,或者如何处理将要加载的数据 MIME:MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型 ...

  10. linux 的常用命令---------第九阶段

    Centos 7 系统启动及相关配置文件(面试题) 1. BIOS 初始化,开始post开机自检(主要检查磁盘.cpu.内存) 2. 加载 MBR 到内存 3. GRUB 阶段(可不说) 4. 加载内 ...