leetcode第15题--3Sum
Problem:
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从第二个开始一直到倒数第二个数,然后left从i的左边一个开始,right从i的右边一个开始,如果三个数相加大于零,那么肯定就要left往左,如果相加小于零那就right往右,如果等于零,再判断是否与已经存入sum中的最后一个判断比较不相等就写入。现在就碰到了以前没有碰到过的问题,不是Time Limit Exceeded,而是Output Limit Exceeded。查了下说,这个问题应该是while循环没有出来。但是我看了以下代码,没有理由没跳出来啊。
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num)
{
bool flag = false;
vector<vector<int> > sum;
vector<int> temp = num;
sort(temp.begin(), temp.end());
if (num.size() < || temp[] > )
return sum; for (int i = ; i != num.size() - ; i++)
{
int left = i - , right = i + ;
while(left>= && right < num.size())
{
if (temp[left] + temp[i] + temp[right] == )
{
vector<int> sub;
sub.push_back(temp[left]);
sub.push_back(temp[i]);
sub.push_back(temp[right]);
if (!flag)
{
flag = true;
sum.push_back(sub);
}
else if(sub != sum[sum.size()-])
{
sum.push_back(sub);
}
left--;
right++;
}
else if(temp[left] + temp[i] + temp[right] < )
{
right++;
}
else
left--;
}
}
sum.erase(unique(sum.begin(), sum.end()), sum.end());
return sum;
}
};
后来请实验室的大牛(可能要去Google工作了)看了下。说应该是内存不够了。就是还是存了相同的例子。单单用判断是不是等于sum的最后一个是否相等是不行的。应该是这样,OJ里测试的时候估计给了sum的大小有限。如果重复了那就Output Limit了。其实我差成功只是一小步了。
以下代码是固定三个数的第一个,然后i从第一个到倒数第三个就行了。如果i往前移动的时候和前一个数相等那就不用再判断了,直接i++,因为以该数开头的三元组都已经计算过并存起来了。其中还有一个很好的地方就是在判断三元组是不是第一个的时候,用的是sum.size()==0 || sum.size() > 0 ...这个很好,因为第一次的时候是==0的,那||之后的就不作判断了。下一次的时候size不==0.再做后面的判断,后面的判断是因为三个数只要有两个数相等那第三个肯定也相等。代码如下。
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int> > sum;
if(num.size()<)
return sum;
sort(num.begin(),num.end());
int k = ;
for(int i = ;i<num.size()-;i++)
{
if(i> && num[i] == num[i-])
continue;
int j = i+; if(num[i]+num[j]>)
break;
k = num.size()-;
while(j<k)
{
if(num[i]+num[j]+num[k] == )
{
if(sum.size()== || sum.size()> && !(num[i]==sum[sum.size()-][]&& num[j] ==sum[sum.size()-][] ))
{
vector<int> ansPiece;
ansPiece.push_back(num[i]);
ansPiece.push_back(num[j]);
ansPiece.push_back(num[k]);
sum.push_back(ansPiece);
}
}
if(num[i]+num[j]+num[k] < )
j++;
else
k--;
}
}
return sum;
}
};
2015/4/3:
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
sort(num.begin(), num.end());
vector<vector<int> > ans;
for (int i = ; i < num.size(); i++){
int target = - * num[i];
if (i > && num[i] == num[i-])
continue;
int left = i + , right = num.size() - ;
while (left < right){
if (target == num[left] + num[right]){
vector<int> tmp;
tmp.push_back(num[i]);
tmp.push_back(num[left]);
tmp.push_back(num[right]);
ans.push_back(tmp);
while(left < num.size() && num[left] == tmp[])
left++;
while(right > && num[right] == tmp[])
right--;
}
else if (target > num[left] + num[right])
left++;
else
right--;
} }
return ans;
}
};
leetcode第15题--3Sum的更多相关文章
- LeetCode第[15]题(Java):3Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
- LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
- [LeetCode][Python]15:3Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 15: 3Sumhttps://oj.leetcode.com/problem ...
- leetcode:1-5题代码整理
以下是这段时间抽时间刷的前5题,都是自己想的解法,或许不是最优解,只是整理下,方便日后优化提升 1. Two Sum: class Solution: # @return a tuple, (inde ...
- leetcode第16题--3Sum Closest
Problem:Given an array S of n integers, find three integers in S such that the sum is closest to a g ...
- LeetCode题解 15题 第二篇
之前写过一篇,这是第二篇.上一篇用了多种编程语言来做,这一次是以学算法为主,所以打算都用python来完成. 4. Median of Two Sorted Arrays There are two ...
- leetcode第15题:三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- LeetCode(15) 3Sum
题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- LeetCode(15)3Sum
题目如下: Python代码: def threeSum(self, nums): res = [] nums.sort() for i in xrange(len(nums)-2): if i &g ...
随机推荐
- ural 1932 The Secret of Identifier (容斥原理)
标题效果: 计算到n字符串. 精确到只是有一个不同的字符,两个不同的字符.三个不同的字符,四对不同的字符. IDEAS: 枚举状态. dp[i] [j] ...当前串取出 i 状态下的全部字符转化成十 ...
- 配置Apacheserver
配置Apacheserver 一.目的 能够有一个測试的server,不是全部的特殊网络服务都能找到免费得! 二.为什么我们要用"Apache"? Apache是眼下使用最广的we ...
- 选中文件夹设定为IIS站点主目录的批处理bat
原文:选中文件夹设定为IIS站点主目录的批处理bat 我使用的OS是winxp,安装的IIS版本为5.1,不支持多站点,下载的一些源代码想测试浏览一下就得设定虚拟目录,而且有些还必须设为站点根目录,每 ...
- validform.js使用方法
表单验证之validform.js使用方法 一.validform有什么用? 网页上有大量的input需要你进行验证的时候,如果是弹窗的话,需要不停地判断,如果为空,弹窗.如果不是数字,弹窗. 所以要 ...
- 【关节点+桥】关节点和桥模板 Tarjan
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; con ...
- CocoaPods停在Analyzing dependencies解决方案
现在很多开源项目应用cocoapod.这使集成第三方库都非常方便,在没有花project里设置哪些参数.仗着. 只要运行pod update要么pod install时间,经常会卡在Analyzing ...
- HDU 4901 The Romantic Hero(二维dp)
题目大意:给你n个数字,然后分成两份,前边的一份里面的元素进行异或,后面的一份里面的元素进行与.分的时候依照给的先后数序取数,后面的里面的全部的元素的下标一定比前面的大.问你有多上种放元素的方法能够使 ...
- Extjs4.1MVC详细解释
:http://xiebaochun.github.io/ app.js [javascript] view plaincopyprint? Ext.onReady(function(){ Ext.Q ...
- MVC中,视图的Layout使用
本文目标 1.能够重用Razor模板进行页面的组件化搭建 本文目录 1.母板页_Layout.cshtml 2.用户自定义控件 3.默认Layout引用的使用(_ViewStart.cshtml) 1 ...
- 项目中经常使用的JS方法汇总,非常有用
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1- ...