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 ...
随机推荐
- C# listbox DataSource数据绑定--一年半以前的bug
listbox使用DataSource进行数据绑定和删除,大家肯定都会, 写这个随笔只是因为....这是一年半以前刚进公司的我遗留的bug,现在看看当时竟然没有解决 - - 现在写个测试程序,写个随笔 ...
- R基础学习(二)-- 连接sqlserver
测试环境:win10+RStudio 三个步骤:(1)创建ODBC数据源:(2)install.packages('RODBC') :(3)编写连接测试脚本 (1)创建ODBC数据源 控制面板-> ...
- 2015-2016 Petrozavodsk Winter Training Camp, Nizhny Novgorod SU Contest (5/9)
2015-2016 Petrozavodsk Winter Training Camp, Nizhny Novgorod SU Contest B. Forcefield 题意 给你一维平面上n个镜子 ...
- hdu 5826 physics 物理题
physics 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5826 Description There are n balls on a smoo ...
- 微信图片生成插件,页面截图插件 html2canvas,截图失真 问题的解决方案
html2canvas 是一个相当不错的 JavaScript 类库,它使用了 html5 和 css3 的一些新功能特性,实现了在客户端对网页进行截图的功能.html2canvas 通过获取页面的 ...
- DbContextScope,A simple and flexible way to manage your Entity Framework DbContext instances,by mehdime
DbContextScope A simple and flexible way to manage your Entity Framework DbContext instances. DbCont ...
- AngularJS中自定义过滤器
AngularJS中为我们提供了一些内置的过滤器,这里列举一些自定义过滤器的场景. 自定义过滤器,不带参赛 //过滤 不带参赛 app.filter('ordinal', function () { ...
- Delphi 完全时尚手册之 Visual Style 篇 (界面不错) 转自http://blog.csdn.net/iseekcode/article/details/4733229
这里先说说两个概念:Theme(主题)和 Visual Style .Theme 最早出现在 Microsoft Plus! for Windows 95 中,是 Windows 中 Wallpape ...
- Xcode 5中非常期待的6个功能
这里是新特征汇总博文链接:iOS7新特征汇总 小引: 自从北京时间2013年06月11日苹果发布Xcode 5 Developer Preview 1,到现在(2013年7约15日)已经过去一个月,苹 ...
- 能添加图标的label
能添加图标的label 效果 源码 https://github.com/YouXianMing/UI-Component-Collection 中的 IconEdgeInsetsLabel // / ...