LeetCode 3Sum (Two pointers)
题意
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)的更多相关文章
- 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 ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [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 ...
- [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 ...
- LeetCode 3Sum Smaller
原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...
- leetcode — 3sum
import java.util.*; /** * Source : https://oj.leetcode.com/problems/3sum/ * * Created by lverpeng on ...
- 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 = ...
- 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 ...
- 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 ...
随机推荐
- AD RMS企业文件版权管理
AD RMS (AD权限管理服务)能够确保企业内部数字文件的机密性,例如,用户即使有权限读取受保护的文件,但是如果未被许可,就无法复制与打印该文件. AD RMS概述 虽然可以通过NTFS权限来设置用 ...
- linux通配符含义
linux通配符含义: . 当前目录**** .. 当前目录的上一级目录**** * 通配符,代表任意0个或多个字符***** ? 通配符,代表重复0个或一个0前面的字符 : ...
- c语言:复合文字
复合文字的意义,相当于是在C语言中,为数组类型定义了一种类似于8之于int,'w'之于char一样的常量类型,所以从C99之后,可以在一些地方使用复合文字来代替数组使用. 复合文字的声明 (]){,, ...
- elasticsearch报错之 memory locking requested for elasticsearch process but memory is not locked
安装elasticsearch报错如下: [2019-01-14T03:57:16,453][ERROR][o.e.b.Bootstrap ] [ip-172-31-30-62.ec2.interna ...
- PyQt5--QToggleButton
# -*- coding:utf-8 -*- ''' Created on Sep 20, 2018 @author: SaShuangYiBing Comment: '' import sys fr ...
- Linux下升级openssl
公司由于使用第三方漏洞扫描,检测出openssh存在漏洞,升级openssh后仍无效果,于是升级openssl 系统信息: 依赖软件:make.gcc.zlib1g-dev 升级步骤: 1.去官网下载 ...
- 2.python数据结构的性能分析
一.引言 - 现在大家对 大O 算法和不同函数之间的差异有了了解.本节的目标是告诉你 Python 列表和字典操作的 大O 性能.然后我们将做一些基于时间的实验来说明每个数据结构的花销和使用这些数据结 ...
- 关于Vue的nextTick的一点小理解
官方文档表示:为了在数据变化之后等待Vue完成更新DOM,可以在数据变化之后立即执行Vue.$nextTick(callback),这样回调函数就可以在数据变化之后立即执行. 这段话的意思是: 例如: ...
- -bash: fork: retry: Resource temporarily unavailable;centos6.5
Last login: Wed Jun 18 14:04:11 2014 from 1.1.1.135 -bash: fork: retry: Resource temporarily unavail ...
- WorldWind源码剖析系列:代理助手类ProxyHelper
代理助手类ProxyHelper通过平台调用的互操作技术封送了若干Win32结构体和函数.该类类图如下. 提供的主要处理方法基本上都是静态函数,简要描述如下: 内嵌类型WINHTTP_AUTOPROX ...