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. sysbench 1.0.9 mysql 压测工具安装使用

    备注:    安装比较简单,可以使用源码或者使用yum 进行安装,本次测试使用yum    注意1.0 之后版本与老版本改动比较大,好多地方都有修改,本次测试使用    的mysql 使用docker ...

  2. vue数据已渲染成 但还是报错 变量 undefined

    问题:页面上的数据已渲染出来,但是控制台还是报错变量未undefined,主要是当页面加载完成后,数据并未加载完,所以会报次错误. 解决办法:在数据渲染的主节点(最外层的div)添加 v-if=“da ...

  3. Codeforces Round #249 (Div. 2)-D

    这场的c实在不想做,sad. D: 标记一下每个点8个方向不经过黑点最多能到达多少个黑点. 由题意可知.三角形都是等腰三角形,那么我们就枚举三角形的顶点. 对于每个定点.有8个方向能够放三角形. 然后 ...

  4. 常见企业IT支撑【4、gitlab代码管理工具】

    安装方式可借鉴http://www.cnblogs.com/juandx/p/5339254.html 安装方式

  5. laravel里面的一些变量

    laravel5里面一些配置,比如数据库,debug的,实际上在项目的.env里面定义过了 //forge是默认值 'database' => env('DB_DATABASE', 'forge ...

  6. Maven和Gradle的比较

    Gradle和Maven都是项目构建工具,但是完全是两个产品,maven应该目前在java企业级开发中占的比重比较大,Gradle是后起之秀,Google的Android Stadio主推的就是Gra ...

  7. Idea项目:Failed to create a Maven project ‘…pom.xml’ already exists in VFS 解决

    在IDEA里面创建Module,因为项目类型原因删掉,又重新创建一个新的,名字没有变.于是报错: Failed to create a Maven project: '**/***/pom.xml' ...

  8. 003:MySQL账号创建授权以及Workbench

    目录 一. 权限管理 1."用户 + IP"的概念 2. 用户权限管理 3. 基本操作 4. 撤销权限 5.授权和创建用户 二. MySQL模拟角色 三. Workbench与Ut ...

  9. python学习 (三十一) python中的class

    1 python的类:   Python类都继承自object. __init__: 构造函数,如果不写,有一个默认的. __init__: 这个构造函数只能有一个,Python中不能有多个构造函数. ...

  10. node的超时timeout

    如果在指定的时间内服务器没有做出响应(可能是网络间连接出现问题,也可能是因为服务器故障或网络防火墙阻止了客户端与服务器的连接),则响应超时,同时触发http.ServerResponse对象的time ...