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 ...
随机推荐
- springboot elk实时日志搭建
https://blog.csdn.net/yy756127197/article/details/78873310 基本的上的过程如这篇博客,logback的配置文件和依赖不太一样 具体见源码其中的 ...
- luogu3809 后缀排序 后缀数组
ref and 挑战程序设计竞赛. 主要是发现自己以前写得代码太难看而且忘光了,而且我字符串死活学不会啊,kmp这种东西我都觉得是省选+难度啊QAQ #include <iostream> ...
- pytorch中设定使用指定的GPU
转自:http://www.cnblogs.com/darkknightzh/p/6836568.html PyTorch默认使用从0开始的GPU,如果GPU0正在运行程序,需要指定其他GPU. 有如 ...
- sql语句中的join连接(左连接、右连接、全连接、内连接)
内部连接(inner join): select * from d_user a inner join D_ORGANIZATION b on a.COMPANY_XID=b.ID 内部链接也是排他 ...
- 公钥密码之RSA密码算法大素数判定:Miller-Rabin判定法!
公钥密码之RSA密码算法大素数判定:Miller-Rabin判定法! 先存档再说,以后实验报告还得打印上交. Miller-Rabin大素数判定对于学算法的人来讲不是什么难事,主要了解其原理. 先来灌 ...
- Dialog共通写法(一个button)
一个button的: package jp.co.hyakujushibank.view import android.app.Dialogimport android.content.Context ...
- K-lord #3 (组合数学)
题目描述 如图,设一个圆分成 n 个扇形 S1 ... ,Sn (扇形大小不一样),现用 k 种不同的颜色对这 n 个扇形进行染色 ( n>=3 , k>=3 ); 每一个扇形染一种颜色, ...
- cf725F Family Photos
Alice and Bonnie are sisters, but they don't like each other very much. So when some old family phot ...
- Chrome中输入框默认样式移除
Chrome中输入框默认样式移除 在chrome浏览器中会默认给页面上的输入框如input.textarea等渲染浏览器自带的边框效果 IE8中效果如下: Chrome中效果如下: 这在我们未给输 ...
- Objective-C NSString的常用用法
//1.创建常量字符串. NSString *astring = @"This is a String!"; //2.创建空字符串,给予赋值. NSString *astrin ...