LeetCode128:Longest Consecutive Sequence
题目:
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given [100, 4, 200, 1, 3, 2]
,
The longest consecutive elements sequence is [1, 2, 3, 4]
. Return its length: 4
.
Your algorithm should run in O(n) complexity.
解题思路:
考虑到题目要求的时间复杂度O(n),很自然的想到哈希表,只有哈希表的查找时间为O(1)。
实现代码:
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std; /*
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in O(n) complexity.
*/
class Solution {
public:
int longestConsecutive(vector<int> &num) {
if(num.empty())
return 0;
unordered_set<int> iset;
vector<int>::iterator iter;
for(iter = num.begin(); iter != num.end(); ++iter)
iset.insert(*iter); int max = 0;
for(iter = num.begin(); iter != num.end(); ++iter)
{
if(iset.count(*iter) == 1)
{
int c = 1;
int g = *iter + 1;
while(iset.count(g) == 1)
{
iset.erase(g);//找到后记得要删除,不然会重复做,超时
c++;
g++;
}
int l = *iter - 1;
while(iset.count(l) == 1)
{
iset.erase(l);
c++;
l--;
}
if(max < c)
max = c; } }
return max;
}
};
int main(void)
{
int arr[] = {100, 4, 200, 1, 3, 2};
int n = sizeof(arr) / sizeof(arr[0]);
vector<int> num(arr, arr+n);
Solution solution;
int max = solution.longestConsecutive(num);
cout<<max<<endl;
return 0;
}
LeetCode128:Longest Consecutive Sequence的更多相关文章
- leetcode解题报告(5):Longest Consecutive Sequence
描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- 【LeetCode-128】Longest Consecutive Sequence
描述 输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 O(n) . 输入 54,55,300,12,56 输出 3 通常我们看有没有连续序列时 找某个数有没有的前后的数,比如看到5 ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
- [LintCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 24. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
随机推荐
- Jenkins 安装或更新插件失败
试试这个插件网址是否可以在网页中打开 http://mirror.xmission.com/jenkins/updates/current/update-center.json 如可以,把这个网址 ...
- 学习之路三十六:SQL知识总结 - [游标||字符串分割]
好久没有写文章了,今天把前不久项目用到的SQL知识总结一下. 一丶字符串分割 SQL内置函数中是没有Split分割函数的,所以需要自己去实现,不多说,上代码: )) RETURNS @result T ...
- TSPL学习笔记(3):排序算法练习
快速排序 快排的详细介绍见,简单的说就是取输入序列中的首元素m,然后将除首元素m以外的其它元素分成两组,小于等于m的一组和大于m的一组.将3组元素组合成输入队列:小于等于m + m + 大于m. 下面 ...
- Winform下richtextbox截图实现
#region 根据richtextbox创建GDI+ private void DrawGDI(RichTextBox rich,Panel panl,PictureBox p2) { rich.U ...
- 计算A/3,不用除法
int DividedBy3(int A) { ; ; i <= ; i += ) p += A << i; return (-p); }
- MariaDB Galera Cluster 10.1 只支持 LINUX ?!
MariaDB Galera Cluster (MariaDB 10.1) 当前只支持:LINUX ! 参考: https://mariadb.com/kb/en/mariadb/getting-s ...
- 日本超人气洛比(Robi)声控机器人
1.日本超人气洛比(Robi)声控机器人. http://technews.cn/2015/04/18/interview-with-robi-creator-tomotaka-takahashi/ ...
- Java帮助文档的生成
首先需要对代码加上文档的注释,比如下面这样: package wz.learning; /** * Title:Person<br> * Description: ...
- 重构第2天:方法搬移(Move Method)
现在就重构来说是非常普通的,虽然我们经常会漏掉或忽略一些需要重构的地方.方法搬移,正如所定义的那样,把方法搬移到更适合他的位置.让我们看看下面这一段重构前的代码: 理解:方法搬移,正如所定义的那样,把 ...
- drupal7 form模板复写方法
给form制作一个template 从官方的drupal api document中可得到form有#theme这个参数,它可以指定form使用一个模板来用于form的基本布局,#theme的值必须是 ...