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.

 
 
利用一个hash表去存储这个数组,对于hash表中的元素,可以去查找比他大的相邻的元素,以及比他小相邻的元素,进而确定最长连续串。
 

以题目给出的数组为例:对于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的更多相关文章

  1. 【leetcode】Longest Consecutive Sequence(hard)☆

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  2. 【LeetCode-128】Longest Consecutive Sequence

    描述 输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 O(n) . 输入 54,55,300,12,56 输出 3 通常我们看有没有连续序列时 找某个数有没有的前后的数,比如看到5 ...

  3. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  4. 【LeetCode OJ】Longest Consecutive Sequence

    Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...

  5. [LeetCode] 128. Longest Consecutive Sequence 解题思路

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  6. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. 【leetcode刷题笔记】Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  8. Java for LeetCode 128 Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

随机推荐

  1. ansible 常用模块

    http://www.linuxidc.com/Linux/2015-02/113068.htm

  2. [转]Oracle中存储过程和函数的区别

    原文地址:http://blog.csdn.net/tender001/article/details/8066203 存储过程和函数: 例子: //创建过程 create or replace pr ...

  3. C#中async/await中的异常处理

    在同步编程中,一旦出现错误就会抛出异常,我们可以使用try-catch来捕捉异常,而未被捕获的异常则会不断向上传递,形成一个简单而统一的错误处理机制.不过对于异步编程来说,异常处理一直是件麻烦的事情, ...

  4. yii2图片上传

    yii2利用自带UploadedFile上传图片 public static function uploadFile($name) { $uploadedFile = UploadedFile::ge ...

  5. springMVC实现防止重复提交

    参考文档:http://my.oschina.net/mushui/blog/143397

  6. poj 3683 2-SAT入门

    原题模型:两者(A,B)不能同时取 #include "cstdio" #include "vector" #include "stack" ...

  7. POJ 3258 River Hopscotch

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11031   Accepted: 4737 ...

  8. SQL增加、删除、更改表中的字段名

    1. 向表中添加新的字段 ) not null 2. 删除表中的一个字段 delete table table_name column column_name 3. 修改表中的一个字段名 alter ...

  9. js的基础学习

    alert() 函数在 JavaScript 中并不常用,但它对于代码测试非常方便. <!DOCTYPE html> <html> <body> <h1> ...

  10. block中防止循环引用的一个高大上的宏定义

    看惯了什么tempSelf weakSelf,来点高大的 #define weakify(...) \ rac_keywordify \ metamacro_foreach_cxt(rac_weaki ...