LeetCode OJ-- 3Sum **
https://oj.leetcode.com/problems/3sum/
在一个数列中,求出所有3个数之和是0的3个数。
刚开始给自己挖了坑,由于没闹清,unique()函数的含义,去掉重复不彻底,所以一直 output limit Exceeded.
于是,想新的判断是否重复的方法,这样跳过了一部分之后,再只判断是否,要加入的数据和上一个加入的数据是否相同,就可以了。
if(i>0 && num[i] == num[i-1])
continue;
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int> > ans;
if(num.size()<)
return ans;
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]>)
continue;
k = num.size()-;
while(j<k)
{
if(num[i]+num[j]+num[k] == )
{
if(ans.size()== || ans.size()> && !(num[i]==ans[ans.size()-][]&& num[j] ==ans[ans.size()-][] ))
{
vector<int> ansPiece;
ansPiece.push_back(num[i]);
ansPiece.push_back(num[j]);
ansPiece.push_back(num[k]);
//remove duplicated
/*bool flag = true;
for(vector<vector<int> >::iterator it = ans.begin(); it != ans.end(); it++)
{
if((*it)[0] == ansPiece[0] && (*it)[1] == ansPiece[1] && (*it)[2] == ansPiece[2])
{
flag = false;
break;
}
}
if(flag)
ans.push_back(ansPiece);*/
ans.push_back(ansPiece);
}
}
if(num[i]+num[j]+num[k] < )
j++;
else
k--;
}
}
return ans;
}
};
LeetCode OJ-- 3Sum **的更多相关文章
- LeetCode OJ 3Sum 3个整数之和
题意:给一个vector<int>容器,要求每当找到3个元素之和为0时就将这3个数按大小顺序记下来,用一个二维vector返回.也就是vector< vector<int> ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
随机推荐
- django-simple-captcha 验证码干扰线随机点位
CAPTCHA_NOISE_FUNCTIONS = ( 'captcha.helpers.noise_null',# 设置样式 'captcha.helpers.noise_arcs',# 设置干扰线 ...
- 用Python和WordCloud绘制词云(内附让字体清晰的秘笈)
环境及模块: Win7 64位 Python 3.6.4 WordCloud 1.5.0 Pillow 5.0.0 Jieba 0.39 目标: 绘制安徽省2018年某些科技项目的词云,直观展示热点. ...
- A1002 A+B for Polynomials (25)(25 分)
1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...
- Java面向对象---类与对象的关系
类是对某一类事物的描述,是抽象的.概念上的意义.对象是实际存在的该类事物的每一个个体,也被称为实例. 创建对象:包括声明对象和实例化对象 Person person;//声明 person = new ...
- loj2074 「JSOI2016」灯塔
loj 题面错的--去bzoj上看吧qwq 观察到 \(\sqrt{|i-j|}\) 的取值只有 \(\sqrt{n}\) 级别个,然后就很显然了,rmq. #include <iostream ...
- cf936c Lock Puzzle
ref #include <algorithm> #include <iostream> #include <cstring> #include <cstdi ...
- 关于html头部引用(meta,link)
/*这一段头部表示 如果安装了GCF,则使用GCF来渲染页面,如果为安装GCF,则使用最高版本的IE内核进行渲染.*/<meta content="IE=edge,chrome=1&q ...
- ogre3D学习基础13 -- 键盘控制网格动画mesh
以上一节为蓝本,这里增加一点难度,添加了四个节点,增加键盘控制移动速度,使用bool变量控制是否移动. 第一,要增加键盘控制,那就使用OIS::KeyListener,在监听器里添加一个父类KeyLi ...
- python 学习分享-实战篇选课系统
# 角色:学校.学员.课程.讲师 # 要求: # 1. 创建北京.上海 2 所学校 # 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 # ...
- [oldboy-django][3作业汇总]相亲网
1 相亲网需求 1相亲网需求 a.登录, 基于session, 装饰器 b.数据库表: 男生表 id, username, password 女生表 id, username, password 约会 ...