leetcode134:3sum
题目描述
注意:
- 三元组(a、b、c)中的元素必须按非降序排列。(即a≤b≤c)
- 解集中不能包含重复的三元组。
例如,给定的数组 S = {-1 0 1 2 -1 -4},解集为(-1, 0, 1) (-1, -1, 2)
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:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- 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)
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &nums) {
vector<vector<int>> res;
sort(nums.begin(),nums.end());
for (int k=0;k<nums.size();++k){
if (nums[k]>0)break;
if (k>0 && nums[k]==nums[k-1]) continue;
int target=0-nums[k];
int i=k+1,j=nums.size()-1;
while (i<j){
if (nums[i]+nums[j]==target){
res.push_back({nums[k],nums[i],nums[j]});
while (i<j && nums[i]==nums[i+1]) ++i;
while (i<j && nums[j]==nums[j-1]) --j;
++i;--j;
}else if (nums[i]+nums[j]<target)++i;
else --j;
}
}
return res;
}
};
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
sort(num.begin(),num.end());
vector <vector<int>> ans;
for (int i=0;i<num.size();i++){
if (i==0 || num[i]!=num[i-1]){
int left=i+1,right=num.size()-1;
while (left<right){
while (left<right && num[i]+num[left]+num[right]>0) right--;
if (left<right && num[i]+num[left]+num[right]==0){
vector<int> temp(3);
temp[0]=num[i];
temp[1]=num[left];
temp[2]=num[right];
ans.push_back(temp);
while (left<right && num[left]==temp[1]) left++;
}else {
left++;
}
}
}
}
return ans;
}
};
leetcode134:3sum的更多相关文章
- 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 ...
- 3Sum algorithm - 非常容易理解的实现 (java)
原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...
- [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 16. 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, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 16. 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 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 ...
随机推荐
- 【题解】[USACO07NOV]Sunscreen G
\(Link\) \(\text{Solution:}\) 把奶牛的忍耐度转化为线段,则题目转化为选择一些点使得覆盖的线段尽可能多.一个点只能覆盖一条线段. 考虑将点按照位置排序,线段按照右端点排序. ...
- 启动你的Android应用:运行设备模拟器和调试代码(第3部分)
下载all source for Test Proj: Test.zip - 306 KB 文章指出 本文包含了我即将出版的新书<启动Android应用程序>中的第三章. 在我完成这本书之 ...
- Linux配置Docker
Centos6.8 1.查看自己的内核 [1].uname [root@host79 ~]# uname -r 2.6.32-642.el6.x86_64 [2].查看CentOS版本信息 CentO ...
- 草率了,又一个Maven打包的问题
经常遇到 Maven 相关的问题,这是之前的文章: 这个 Maven 依赖的问题,你敢说你没遇到过:https://mp.weixin.qq.com/s/SzBbDtyRUrk_7LH8SUbGXQ ...
- 题解:[COCI2011-2012#5] BLOKOVI
题解:[COCI2011-2012#5] BLOKOVI Description PDF : https://hsin.hr/coci/archive/2011_2012/contest5_tasks ...
- centos8上安装openresty
一,openresty的官网地址: http://openresty.org/ 说明:说一下openresty的安装方式: 从openresty的安装目录下,可以看到openresty编译安装了自己作 ...
- Java9系列第6篇-Stream流API的增强
我计划在后续的一段时间内,写一系列关于java 9的文章,虽然java 9 不像Java 8或者Java 11那样的核心java版本,但是还是有很多的特性值得关注.期待您能关注我,我将把java 9 ...
- Postgres 10.11安装教程
Postgres搭建 考虑到Drone和Sonarqube等都需要借助Postgres存储,为了保证CI服务的高效的工作,将考虑独立其中依赖的数据库,避免在繁忙时占用过多资源从而影响整体CI的速度. ...
- 通过SQL自动添加流水号
通过SQL自动添加流水号 项目中往往有一些单据流水号或者流程流水号是希望通过新增一条记录后自动产生一个编号的,比如新增一条流程就自动根据当前日期自动添加该流程的流程流水号,下面介绍两种不同类型流水号通 ...
- js 如何获取浏览器的高度?
<SCRIPT LANGUAGE="JavaScript"><!--var s = ""; s += " 网页可见区域宽:" ...