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 ...
随机推荐
- Linux下安装MySQLdb模块(Python)
一.MySQLdb-python模块 https://pypi.python.org/pypi/MySQL-python ` 二.安装依赖包 yum -y install python-devel m ...
- Dell Idrac Normal Settings
racadm安装请查看:http://www.cnblogs.com/zyd112/p/7611022.html racadm语法(远程执行命令):racadm -r <racIpAddr> ...
- 【LeetCode】Unique Email Addresses(独特的电子邮件地址)
这道题是LeetCode里的第929道题. 题目要求: 每封电子邮件都由一个本地名称和一个域名组成,以 @ 符号分隔. 例如,在 alice@leetcode.com中, alice 是本地名称,而 ...
- C. RMQ with Shifts
C. RMQ with Shifts Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 131072KB 64-bit intege ...
- TOJ 5020: Palindromic Paths
5020: Palindromic Paths Time Limit(Common/Java):10000MS/30000MS Memory Limit:65536KByteTotal Su ...
- TheBrain8破解方式
破解文件下载地址:http://rghost.net/51736270 mac破解方式: 我用的MAC 装的8007版本的,今天竟然提示要升级专业版本了.补救方法是,先打开TB,把之前手贱输入的云服务 ...
- 虚拟机搭建--hyper-V使用教程
http://jingyan.baidu.com/article/4e5b3e19695d9f91901e24bb.html
- centos7 下修改网络配置
修改ip地址 编辑 /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet BOOTPROTO=static 静态ip DEFROUTE=yes ...
- UVa1363 Joseph's Problem
把整个序列进行拆分成[k,k/2),[k/2, k/3), [k/3,k/4)...k[k/a, k/b)的形式,对于k/i(整除)相同的项,k%i成等差数列. /*by SilverN*/ #inc ...
- svn服务安装与配置
SVN安装 centos系统下执行yum install subversion 创建项目 svnadmin create dxk-test 创建项目dxk-test 服务配置与权限控制 vim con ...