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 ...
随机推荐
- ECMASctipt6总结
1.let 变量声明以及特性 声明变量 let a; let b, c, d; let e = 1; let f = 2, g = 3; 特性 1.不能重复声明 2.块级作用域 只在块级作用域有效 ...
- javascript 数据类型判断总结
一 typeof 回顾:js有五种基本数据类型:值类型("number","string","boolean","undefine ...
- 正式班D7
2020.10.13星期二 正式班D7 一.上节课复习 Linux发展 批处理系统 多道技术 分时操作系统 multics->Unix->minix->Linux(如Redhat.c ...
- 8.Android-简单的登录案例编写
本章来学习登录案例,由于还未学习自定义控件外观,所以ui界面先用最简单的,并保存登录账号密码到data/data/包名/files下 1.学习之前需要掌握的Context类(通过Context来往AP ...
- 微信聊天记录导出为csv,并生成词云图
微信聊天记录生成特定图片图云 首先贴上github地址 https://github.com/ghdefe/WechatRecordToWordCloud 来个效果图 提取聊天记录到csv参考教程 h ...
- java 画 哆啦A梦
package Demo;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;impo ...
- [转] Swoft HTTP 服务
转载自Go语言中文网, https://studygolang.com/articles/20667 传统架构 PHP-FPM + Nginx 传统架构中所使用的Nginx + PHP-FPM的模型中 ...
- go 结构体初始化
package main import "fmt" type Dog struct { Name string } func TestStruct() { // 方式1 //var ...
- win安装appium
Windows 下配置 Appium,要提前装好jdk,请参考:https://jingyan.baidu.com/article/e8cdb32b2699cb37042bad59.html 1.下载 ...
- 最全Python基础知识点梳理
本文主要介绍一些平时经常会用到的python基础知识点,用于加深印象,也算是对于学习这门语言的一个总结与回顾.python的详细语法介绍可以查看官方编程手册,也有一些在线网站可以学习 python语言 ...