【leetcode】Longest Consecutive Sequence
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.
以题目给出的数组为例:对于100,先向下查找99没找到,然后向上查找101也没找到,那么连续长度是1,从哈希表中删除100;然后是4,向下查找找到3,2,1,向上没有找到5,那么连续长度是4,从哈希表中删除4,3,2,1。这样对哈希表中已存在的某个元素向上和向下查找,直到哈希表为空。算法相当于遍历了一遍数组,然后再遍历了一遍哈希表,复杂的为O(n)
class Solution {
public:
int longestConsecutive(vector<int> &num) { unordered_set<int> hash;
unordered_set<int>::iterator it; for(int i=;i<num.size();i++) hash.insert(num[i]); int count;
int result=;
while(!hash.empty())
{
count=; it=hash.begin();
int num0=*it;
hash.erase(num0); int num=num0+;
while(hash.find(num)!=hash.end())
{
count++;
hash.erase(num);
num++;
} num=num0-;
while(hash.find(num)!=hash.end())
{
count++;
hash.erase(num);
num--;
} if(result<count) result=count;
} return result;
}
};
【leetcode】Longest Consecutive Sequence的更多相关文章
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【LeetCode-128】Longest Consecutive Sequence
描述 输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 O(n) . 输入 54,55,300,12,56 输出 3 通常我们看有没有连续序列时 找某个数有没有的前后的数,比如看到5 ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode刷题笔记】Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
随机推荐
- 在CentOS7上安装RabbitMQ
安装过程参考官网: Installing on RPM-based Linux (RHEL, CentOS, Fedora, openSUSE) 首先需要安装erlang,参考:http://fedo ...
- word-break:brea-all;word-wrap:break-word的区别
//form==>http://www.cnblogs.com/2050/archive/2012/08/10/2632256.html <p style="background ...
- Oracle 调度程序(scheduler)摘自一位大神
在11g中,Oracle提供了一个新建的Scheduler特性,帮助将作业实现自动化.它还可以帮助你控制资源的利用与并可以将数据库中的作业按优先顺序执行.传统的dbms_jobs的一个限制是它只能调度 ...
- session实现防止重复提交,以及验证
参考文档 1.生成Token的参考文档.http://www.cnblogs.com/TianFang/p/3180899.html 2.主要参考文档.http://www.cnblogs.com/x ...
- 【poj3348】 Cows
http://poj.org/problem?id=3348 (题目链接) 题意 给出平面上n个点,以这n个点中的一些围成的多边形面积 div 50的最大值. Solution 凸包求面积. 很好做, ...
- html中设置锚点定位的几种常见方法(#号定位)
在html中设置锚点定位我知道的有几种方法,在此和大家分享一下: 1.使用id定位: <a href="#1F">锚点1</a> <div id=&q ...
- PowerDesigner15下载、安装以及破解
一.先安装PowerDesigner15(PowerDesigner15.1.0.2850),下载地址:点击下载 二.破解文件下载地址: 找到一个,居然这家伙的东西不是免费的:点击跳转 三.破解方法: ...
- POJ1651Multiplication Puzzle(矩阵链乘变形)
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8040 Accepted: ...
- MyEclipse------缓冲流的使用
可以把BufferedReader和BufferedWriter称为上层流,把它们指向的字符流(Reader,Writer)称为底层流. Java采用缓存技术将上层流和底层流连接. 底层字符输入流先将 ...
- 深入理解 Javascript 面向对象编程
一:理解构造函数原型(prototype)机制 prototype是javascript实现与管理继承的一种机制,也是面向对象的设计思想.构造函数的原型存储着引用对象的一个指针,该指针指向与一个原型对 ...