LeetCode——3Sum
1. Question
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: 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]
]
2. Solution
先排序,然后对于每一个nums[i],在其后找到两个数字num[j]和num[k],j<k,使得三者和为0。
去掉重复的组合,如果满足三者为0,那么j需要后移到一个不同的数,k也需要前移到一个不同的数。
重复的nums[i]也需要去掉,因为nums[i]和nums[i+1] 都是和其后的进行组合,如果两个一样的话,就重复了,需要去掉。
3. Code
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
if (nums.size() <= 2)
return vector<vector<int>>();
sort(nums.begin(), nums.end());
vector<vector<int>> res;
for (int i = 0; i < nums.size() - 2; i++) {
int start = i + 1;
int end = nums.size() - 1;
while (start < end) {
int tmp = nums[start] + nums[end];
int target = -nums[i];
if (tmp == target) {
vector<int> ele;
ele.push_back(nums[i]);
ele.push_back(nums[start]);
ele.push_back(nums[end]);
res.push_back(ele);
// 去掉start重复的
while (start < end && nums[start] == ele[1])
start++;
// 去掉end重复的
while (end > start && nums[end] == ele[2])
end--;
} else if (tmp > target)
end--;
else
start++;
}
// 去掉nums[i]重复的
while (i + 1 < nums.size() - 2 && nums[i + 1] == nums[i])
i++;
}
return res;
}
};
LeetCode——3Sum的更多相关文章
- [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: 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 ...
- 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
1.题目描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- Leetcode 3Sum Closet
二手和3Sum像几乎相同的想法.二进制搜索.关键修剪.但是,在修剪做出很多错误. 然后还有一个更加速了原来的想法O(n^2). #include<iostream> #include &l ...
随机推荐
- However, a general purpose protocol or its implementation sometimes does not scale very well.
https://netty.io/wiki/user-guide-for-4.x.html The Problem Nowadays we use general purpose applicatio ...
- 16.Update Methods-官方文档摘录
这里没什么好说的,直接贴文了 MongoDB provides the following methods for updating documents in a collection: db.col ...
- MySQL优化(三):优化数据库对象
二.优化数据库对象 1.优化表的数据类型 应用设计的时候需要考虑字段的长度留有一定的冗余,但不推荐很多字段都留有大量的冗余,这样既浪费磁盘空间,也在应用操作时浪费物理内存. 在MySQL中,可以使用函 ...
- 浅谈Spark2.x中的Structured Streaming
在Spark2.x中,Spark Streaming获得了比较全面的升级,称为Structured Streaming,和之前的很不同,功能更强大,效率更高,跟其他的组件整合性也更好. 连续应用程序c ...
- Mysql-xtrabackup 与MySQL5.7 binlog 实现数据即时点恢复
Mysql-xtrabackup 与MySQL5.7 binlog 实现数据即时点恢复 一.数据库准备 1. rpm -e mariadb-libs postfix tar xf mysql-5.7 ...
- 模块讲解----json模块(跨平台的序列化与反序列化)
一.json的特点 1.只能处理简单的可序列化的对象:(字典,列表,元祖) 2.json支持不同语言之间的数据交互:(python - go,python - java) 二.使用场景 1.玩 ...
- 搭建virtualenv
一.前言 1.什么是virtualenv? 在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-packages目 ...
- windows安装redis, php5.5
全套安装包地址 http://download.csdn.net/detail/whellote/9572797 解压 redis-2.2.5-win32-win64, 将里面的内容拷贝到j:/r ...
- maven tomcat7-maven-plugin配置及背景
背景: 在研发阶段,想让一个服务通过tomcat启动起来有很多的方法,常用的idea都有这样的支持,那么如果我们没有tomcat,能不能让服务通过tomcat启动起来呢?maven就提供了这样的支持. ...
- HDU - 4609 3-idiots (FFT+母函数)
题意:给N个数,求任意选三个数能构成三角形的概率 分析:枚举两条边之和的复杂度\(O(N^2)\),显然不行,所以要更高效地做到枚举出两边之和. 所以用生成函数搭配FFT在\(O(NlogN)\)的时 ...