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),遍历到某个数据,可以向前和向后找它的连续序列。再用一个哈希表存储已访问过的元素,这样保证每个元素至多被处理一次。

哈希表在C++中用unordered_set实现。set的实现是红黑树,插入查找删除的时间复杂度是O(logn)不能使用。

class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> visited;
unordered_set<int> exist;
int ret = ;
int count;
int target; for(int i = ; i < nums.size(); i++){
exist.insert(nums[i]);
} for(int i = ; i < nums.size(); i++){
if(visited.find(nums[i])!=visited.end()) continue; visited.insert(nums[i]);
count = ;
target = nums[i];
while(exist.find(--target)!=visited.end()){
visited.insert(target);
count++;
}
target = nums[i];
while(exist.find(++target)!=visited.end()){
visited.insert(target);
count++;
}
if(count > ret) ret = count;
}
return ret;
}
};

128. Longest Consecutive Sequence (HashTable)的更多相关文章

  1. 128. Longest Consecutive Sequence(leetcode)

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

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

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

  3. 【LeetCode】128. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

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

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

  5. 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度

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

  6. leetcode 128. Longest Consecutive Sequence ----- java

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

  7. 128. Longest Consecutive Sequence

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

  8. [leetcode]128. Longest Consecutive Sequence最长连续序列

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

  9. 128. Longest Consecutive Sequence最长连续序列

    [抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

随机推荐

  1. quick 状态机StateMachine

    function Player:addStateMachine() self.fsm_ = {} cc.GameObject.extend(self.fsm_) :addComponent(" ...

  2. counting elements--codility

    lesson 4: counting elements 1. FrogRiverOne 2. PermCheck 3. MissingInteger 4. MaxCounters lesson 4: ...

  3. c#通过app.manifest使程序 右键 以管理员身份运行

    c#通过app.manifest使程序以管理员身份运行 时间:2013-06-27 22:47来源:网络收集+本站整理 作者:jtydl 点击: 1175 次 微软在Windows Vista开始引入 ...

  4. java中Mongo

    1.  query.fields().include("idfa").include("imei").include("id").inclu ...

  5. 32位centos下安装jdk1.7报Permission denied处理方式

    本文转载自:http://blog.csdn.net/snowwhitewolf/article/details/50287877 环境:centos5.8 32位jdk-7u71-Linux-i58 ...

  6. 第三方登录之微信登录,基于ThinkSDK

    本文基于ThinkSDK,为其补充微信登录demo 增加ThinkSDK的微信第三方登录 阅读本文之前请先了解ThinkSDK的文档 http://www.echomod.com/nexstep/fo ...

  7. 第三章:Hadoop简介及配置Hadoop-1.2.1,hbase-0.94.13集群

    前面给大家讲了怎么安装Hadoop,肯定会有人还是很迷茫,装完以后原来就是这个样子,但是怎么用,下面,先给大家讲下Hadoop简介:大致理解下就OK了 hadoop是一个平台,提供了庞大的存储和并行计 ...

  8. Code blocks调试教程

    特别声明:本教程已转移至百度经验:https://jingyan.baidu.com/article/6b182309939a87ba58e159bf.html 一.题外话 之前一直想用Code bl ...

  9. UISegmentedControl-iOS

    //建立UISegmentedControl的数组 NSArray *segmentedArray = [NSArray arrayWithObjects:@"线下培训",@&qu ...

  10. 11.solr学习速成之MoreLikeThis

    Solr相似匹配    在网页搜索或电商产品搜索结果页面,很多时候会看到一个相似文档.相似产品或找相似的链接.Solr 使用 MoreLikeThisComponent(MLT)和 MoreLikeT ...