题意:

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. [转载]关于CSDN, cnblog, iteye和51cto四个博客网站的比较与分析

    CSDN:http://blog.csdn.net/ cnblog: http://www.cnblogs.com/ iteye: http://www.iteye.com/blogs/ 51cto: ...

  2. svn's tree conflict

    [svn's tree conflict] A tree conflict occurs when a developer moved/renamed/deleted a file or folder ...

  3. KMP应用http://acm.hdu.edu.cn/showproblem.php?pid=2594

    riemann与marjorie拼接后riemannmarjorie前缀与后缀公共部分为 rie 长度为 3(即next[l] = next[14]的值,l为拼接后的长度)但:aaaa与aa拼接后aa ...

  4. UVa11997K Smallest Sums(优先队列)

    K Smallest Sums You're given k arrays, each array has k integers. There are kk ways to pick exactly ...

  5. POJ1185状态压缩DP

    难得的中文题. POJ1185http://poj.org/problem?id=1185 方法就是用DP[i][r][p]表示第i行状态为r,第i-1行状态是p时的最多个数.而这里p受到r的限制,而 ...

  6. C# Devexpress学习--LabelControl

    A LabelControl can display an image (regular or animated GIF file). Different images can be provided ...

  7. 配置Synergy(Server : XP, client: Win7)

    此文只是为了Mark一下配置方法,以防以后重装系统的时候,忘记.   首先,因为我的Server机器是XP,所以要求两台机器,都是安装的x86的版本,而不能是x64的版本. 我用的版本是1.4.11, ...

  8. IIS 7 WAS服务不可用

    在 Windows Server 2008 上使用 %windir%\system32\inetsrv\appcmd.exe list wp 命令,得到如下错误: ERROR ( message:WA ...

  9. ios transition translate 闪屏问题总结

    webkit在绘制页面时会将结构分为各种层,当层足够大时就会变成很大的平铺层.这样一来webkit在每次页面结构发生变化时不需要都渲染整个页面而是渲染对应层了,这对渲染速度来说相当的重要.webkit ...

  10. Spring MVC防止数据重复提交

    现实开发中表单重复提交的例子很多,就包括手上这个门户的项目也有这种应用场景,用的次数多,但是总结,这还是第一次. 一.基本原理 使用token,给所有的url加一个拦截器,在拦截器里面用java的UU ...