LeetCode_3Sum
一.题目
3Sum
Total Accepted: 45112 Total
Submissions: 267165My
Submissions
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)
二.解题技巧
要保证不出现反复的情况,当i!=0时,假设第i个数与第i-1个数同样的话,则不进行处理,直接处理第i+1个元素。这样。仅仅要保证三个数中最小的数是依照顺序递增的。那么算法找到的解就都是不反复的。
三.实现代码
class Solution
{
public:
vector<vector<int> > threeSum(vector<int> &num)
{
int Size = num.size(); vector<vector<int> > Result; if (Size < 3)
{
return Result;
} sort(num.begin(), num.end()); for (int Index_outter = 0; Index_outter < (Size - 2); Index_outter++)
{
int First = num[Index_outter];
int Second = num[Index_outter + 1];
const int Target = 0; if ((Index_outter != 0) && (First == num[Index_outter - 1]))
{
continue;
} int Start = Index_outter + 1;
int End = Size - 1; while (Start < End)
{
Second = num[Start];
int Third = num[End]; int Sum = First + Second + Third; if (Sum == Target)
{
vector<int> Tmp;
Tmp.push_back(First);
Tmp.push_back(Second);
Tmp.push_back(Third); Result.push_back(Tmp); Start++;
End--;
while (num[Start] == num[Start - 1])
{
Start++;
}
while (num[End] == num[End + 1])
{
End--;
} } if (Sum < Target)
{
Start++;
while (num[Start] == num[Start -1])
{
Start++;
}
} if (Sum > Target)
{
End--;
if (num[End] == num[End + 1])
{
End--;
}
} } }
return Result; }
};
四.体会
版权全部,欢迎转载,转载请注明出处。谢谢

LeetCode_3Sum的更多相关文章
- LeetCode_3Sum Closest
一.题目 3Sum Closest Total Accepted: 32191 Total Submissions: 119262My Submissions Given an array S of ...
随机推荐
- seajs模块化加载框架使用
seajs是模块化加载框架.seajs.org已经打不开了,seajs的github.seajs速查文档 <!--如果完成下面4步,则seajs掌握了80%js模块化1.引入seajs的库 :& ...
- Jmeter接口测试-简单分析结果数、聚合报告以及图形结果(二)
简单分析结果数.聚合报告以及图形结果 结果树 取样器结果:返回值报200,表示执行接口调试成功 请求:发送的数据 响应数据:返回的数据 Thread Name:线程组名称 Sample Start: ...
- mongodb权限机制以及扩展
mongodb权限机制 启动权限机制之前要先在MONGODB中添加管理员账号: 1. 创建账号 重装安装一个mongodb,安装时添加一个 --auth参数: 先把安装好的从服务中删除掉(删除之后数据 ...
- [uiautomator篇][python] wifi接口学习网址
https://wifi.readthedocs.io/en/latest/wifi_command.html#usage
- Codeforces Round #362 (Div. 2)
闲来无事一套CF啊,我觉得这几个题还是有套路的,但是很明显,这个题并不难 A. Pineapple Incident time limit per test 1 second memory limit ...
- Codeforces Round #415 (Div. 2) 翻车啦
A. Straight «A» time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- TOJ 假题之 Cow Brainiacs
1570: Cow Brainiacs Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Submit: ...
- 84. Spring Boot集成MongoDB【从零开始学Spring Boot】
至于MongoDB网上有很多相关的资料,所以在这里不进行过多的介绍,我们在这里主要是介绍下如何将mongodb与spring boot结合使用.本节大纲: (1) 准备工作: (2) 新建一个mave ...
- 关于安卓浏览器无法识别es6语法
这几天写代码,在highcharts的代码里用了一些es语法 在PC端及iphone上都能正常运行,在安卓上无法显示 一直不知道什么原因.后来一点点查看才发现是下面的两句es6代码 1: .map(i ...
- log4j中isDebugEnabled(), log.isInfoEnabled()和log.isTraceEnabled的封装
1. log4j中log.isDebugEnabled(), log.isInfoEnabled()和log.isTraceEnabled()作用 项目在应用log4j打印Debug,Info和Tra ...