57. 三数之和 &&
题目
57. 三数之和
给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。
样例
如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集合的是:
(-1, 0, 1)
(-1, -1, 2)
注意事项
在三元组(a, b, c),要求a <= b <= c。
结果不能包含重复的三元组。
解析
- 主要注意去除重复元素的方法
class Solution_57 {
public:
/**
* @param numbers: Give an array numbers of n integer
* @return: Find all unique triplets in the array which gives the sum of zero.
*/
vector<vector<int>> threeSum(vector<int> &numbers) {
// write your code here
vector<vector<int>> ret;
if (numbers.size() < 3)
return ret;
sort(numbers.begin(), numbers.end());
vector<int> vec;
for (int i = 0; i < numbers.size() - 2; i++)
{
if (i >= 1 && numbers[i - 1] == numbers[i]) //去重复元素
{
continue;
}
int b = i + 1;
int c = numbers.size() - 1;
while (b<c)
{
while (b<c&&c<numbers.size() - 1 && numbers[c + 1] == numbers[c]) //去重只需要在查找到过后进行,所以放在下面else里面,容易理解一些
c--;
while (b<c&&b - 1>i&&numbers[b - 1] == numbers[b])
b++;
if (b == c)
continue;
int sum = numbers[i] + numbers[b] + numbers[c];
if (sum>0)
{
c--;
}
else if (sum < 0)
{
b++;
}
else
{
vec.clear();
vec.push_back(numbers[i]);
vec.push_back(numbers[b]);
vec.push_back(numbers[c]);
ret.push_back(vec);
//break; //bug
c--;
b++;
}
}
}
return ret;
}
vector<vector<int>> threeSum(vector<int> &numbers) {
// write your code here
vector<vector<int>> ret;
if (numbers.size() < 3)
return ret;
sort(numbers.begin(), numbers.end());
vector<int> vec;
for (int i = 0; i < numbers.size() - 2; i++)
{
if (i >= 1 && numbers[i - 1] == numbers[i]) //去重复元素
{
continue;
}
int b = i + 1;
int c = numbers.size() - 1;
while (b<c)
{
int sum = numbers[i] + numbers[b] + numbers[c];
if (sum>0)
{
c--;
}
else if (sum < 0)
{
b++;
}
else
{
vec.clear();
vec.push_back(numbers[i]);
vec.push_back(numbers[b]);
vec.push_back(numbers[c]);
ret.push_back(vec);
//break; //bug
c--;
b++;
while (b < c&&numbers[c + 1] == numbers[c])
c--;
while (b < c&&numbers[b - 1] == numbers[b])
b++;
}
}
}
return ret;
}
};
57. 三数之和 &&的更多相关文章
- [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 ...
- lintcode: 三数之和II
题目 三数之和 II 给一个包含n个整数的数组S, 找到和与给定整数target最接近的三元组,返回这三个数的和. 样例 例如S = . 和最接近1的三元组是 -1 + 2 + 1 = 2. 注意 ...
- lintcode:三数之和
题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...
- 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 ...
- LeeCode数组第15题三数之和
题目:三数之和 内容: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...
- LeetCode第十六题-找出数组中三数之和最接近目标值的答案
3Sum Closest 问题简介: 给定n个整数的数组nums和整数目标,在nums中找到三个整数,使得总和最接近目标,返回三个整数的总和,可以假设每个输入都只有一个解决方案 举例: 给定数组:nu ...
- 南大算法设计与分析课程OJ答案代码(4)--变位词、三数之和
问题 A: 变位词 时间限制: 2 Sec 内存限制: 10 MB提交: 322 解决: 59提交 状态 算法问答 题目描述 请大家在做oj题之前,仔细阅读关于抄袭的说明http://www.bi ...
随机推荐
- android 上下边框线
<!-- 连框颜色值 --> <item> <shape> <solid android:color="@android:color/backgro ...
- Kernel 4.9的BBR拥塞控制算法与锐速
重要的事情说三遍! BBR并不能突破带宽限制!!! BBR并不能突破带宽限制!!! BBR并不能突破带宽限制!!! 它的功能如下: 1.在高丢包率与低速率的网络中提升传输效果,充分利用带宽. 2.降低 ...
- 10分钟上手python pandas
目录 Environment 开始 对象创建 查看数据 选择 直接选择 按标签选择 按位置选择 布尔索引 设置 缺失数据 操作 统计 应用(apply) 直方图化(Histogramming) 字符串 ...
- Maven入门指南③:坐标和依赖
1 . 坐标 maven 的所有构件均通过坐标进行组织和管理.maven 的坐标通过 5 个元素进行定义,其中 groupId.artifactId.version 是必须的,packaging 是可 ...
- Meta对照表
Http Content_type对照表: 文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Type) .*( 二进制流,不知道下载文件类型 ...
- YAML文件中在单一文件中区分多个文件
1.在单一文件中,可用连续三个连字号(---)区分多个文件. 2.另外,还有选择性的连续三个点号( ... )用来表示文件结尾. 题外:YAML其实语法很多也很灵活,但是针对Spring支持的语法其实 ...
- ST推出新软件STM32Cube ,让STM32微控制器应用设计变得更容易、更快、更好用
功能强大的STM32Cube 新软件平台由设计工具.中间件和硬件抽象层组成,让客户能够集中精力创新 意法半导体(STMicroelectronics,简称ST)针对STM32微控制器推出一套免费的功能 ...
- CVPR 2017
https://www.leiphone.com/news/201707/5D5qSICrej6xIdzJ.html Densely Connected Convolutional Networks ...
- win10系统更新不了,总出现错误0xc8000442
来自 win10系统更新不了,总出现错误0xc8000442,SHIZHI333的回答. 首先卸载制有第三方防护软件管管家类软件,再试试下面的方法:清理一下更新临时文件,具体操作如下: 1.右键点击开 ...
- C#编程(五十五)----------HashSet和SortedSet
集 饱含不重复元素的集合称为”集(set)”. .NET4包含两个集(HashSet<T>和SortedSet<T>),他们都实现ISet<T>接口.HashSet ...