题意:

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.  (Medium)

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]

分析:

暴力搜索是O(n^3),利用2sum思路,三个元素相加为零,即两个元素相加等于另一个元素的相反数。

所以先排序,然后确定搜索顺序,对每一个元素,从其下一位和最后一个元素开始向中间搜索(类似two sum), 时间复杂度O(n^2)

注意问题:

1. 元素个数小于三个时候,直接返回空vector;

2. 遇到相同的选定时候需要跳过;

3. 遇到相同的待加数跳过(例如 0,-1,-1,1中,不跳过会出现两个0,-1,1在结果中)

4. 对于已经找到一组解之后,自己考虑的方案是,只把start++, 所以对于第3点的判定是判定nums[start]与nums[start -1]是否相同即可;

参考讨论区中的方案是,找到一组解之后,start++,end--只到与前一位不重复,可以作为参考。

代码1:

 class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> v;
if (nums.size() < ) {
return v;
}
for (int i = ; i < nums.size() - ; ++i) {
if (i >= && nums[i] == nums[i - ]) { //遇到相同的选定跳过
continue;
}
int start = i + , end = nums.size() - ;
while (start < end) {
if ( nums[i] + nums[start] + nums[end] == ) {
if (start > i + && nums[start] == nums[start - ]) { //[0,0,0,0]情况, 相同的相加元素
start++;
continue;
}
vector<int> temp{nums[i], nums[start], nums[end]};
v.push_back(temp);
start++;
}
else if (nums[i] + nums[start] + nums[end] > ) {
end--;
}
else {
start++;
}
}
}
return v;
}
};

代码2:

 class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> v;
if (nums.size() < ) { //数组元素个数过少,直接返回
return v;
}
for (int i = ; i < nums.size() - ; ++i) {
if (i >= && nums[i] == nums[i - ]) {
continue;
}
int start = i + , end = nums.size() - ;
while (start < end) {
if ( nums[i] + nums[start] + nums[end] == ) {
vector<int> temp{nums[i], nums[start], nums[end]};
v.push_back(temp);
start++;
end--;
while ( (start < end) && nums[start] == nums[start - ]) { //没加start < end虽然过了,估计是样例不够完善
start++;
}
while ( (start < end) && nums[end] == nums[end + ]) {
end--;
}
}
else if (nums[i] + nums[start] + nums[end] > ) {
end--;
}
else {
start++;
}
}
}
return v;
}
};
												

LeetCode15 3Sum的更多相关文章

  1. leetcode15—3Sum

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  2. LeetCode15——3Sum

    数组中找三个数和为0的结果集 1 // 解法一:先排序 然后固定一个值 然后用求两个数的和的方式 public static List<List<Integer>> three ...

  3. Leetcode15.3Sum三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  4. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  5. ARTS第六周

    第六周.后期补完,太忙了. 1.Algorithm:每周至少做一个 leetcode 的算法题2.Review:阅读并点评至少一篇英文技术文章3.Tip:学习至少一个技术技巧4.Share:分享一篇有 ...

  6. [Swift]LeetCode15. 三数之和 | 3Sum

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  7. 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 ...

  8. 3Sum algorithm - 非常容易理解的实现 (java)

    原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...

  9. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

随机推荐

  1. 加固Samba安全三法

    欢迎大家给我投票: http://2010blog.51cto.com/350944 650) this.width=650;" onclick='window.open("htt ...

  2. 一、 使用存储过程实现数据分页(Sql Server 2008 R2)

    1.废话不多说了,直接上代码.调用这个存储过程只需要传递 表名,排序字段,搜索字段,以及页码,页码数量,搜索值(可空) create PROCEDURE NewPage --通用的分页存储过程,百万数 ...

  3. Ubuntu之系统交换分区Swap增加与优化

    http://os.51cto.com/art/201212/372860.htm   http://blog.csdn.net/xingyu15/article/details/5570225   ...

  4. 使用paramiko来实现sftp

    sftp是一个基于ssh的文件传输协议,是在Windows上往linux传送文件最常用的方式(例如SecureFX,Xftp). 在python下,paramiko实现了sftp,可以让大家方便地在代 ...

  5. VSTO安装部署(完美解决XP+2007)

    从开始写VSTO的插件开始,安装部署一直就是一个很大的难题,其实难题的原因主要是针对XP+2007而言.在Win7上,由于基本上都预装了.net framework,所以安装起来其实问题不大. 主要需 ...

  6. 获取和设置localStorage

    东钿金融服务平台 用户第一次访问页面出现,引导步骤,起初一直使用cookie,但是cookie一直不稳定 今天老大说改用localStorage 于是乎百度,查了一篇博客 http://www.cnb ...

  7. SQL SERVER 2005如何建立自动备份的维护计划

    SQL Server 2005中可以使用维护计划来为数据库自动备份,减少数据库管理员的工作负担.其使用方法如下: (1)启动[sql server Management Studio],在[对象资源管 ...

  8. CentOS FireFox Flash Player

    yum install *firefox* yum install flash-plugin

  9. HTML第六天学习笔记

    今天主要对思维导图笔记进行了整理与更新:

  10. VC++内存区域

    转载声明:本文转载自http://blog.csdn.net/sjxbf/article/details/6441730 一.预备知识—程序的内存分配 一个由c/c++编译的程序占用的内存分为以下几个 ...