128. Longest Consecutive Sequence (HashTable)
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)的更多相关文章
- 128. Longest Consecutive Sequence(leetcode)
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】128. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 128. 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 ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 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. Y ...
- 128. Longest Consecutive Sequence最长连续序列
[抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
随机推荐
- android 自己定义checkbox 背景图无效的问题
http://blog.csdn.net/zuolongsnail/article/details/7106586 正常的定义能够參考这个网址 可是我參考它以后发现我执行时候 根本不工作嘛 结果 ...
- 基于C#的UDP协议的异步实现
一.摘要 总结UDP传输协议的异步实现. 二.实验平台 visual studio 2010 三.实验实例 服务器端代码: using System; using System.Collections ...
- C#数据路接口中获取SQL数据的用法
获取一条记录 string sql = string.Format(“”); DataRow row; if (GetFirstDataRow(sql, out row)) { ...
- Oracle11g的服务
成功安装Oracle 11g数据库后,你会发现自己电脑运行速度会变慢,配置较低的电脑甚至出现非常卡的状况,通过禁止非必须开启的Oracle服务可以提升电脑的运行速度.那么,具体该怎么做呢.按照win7 ...
- RK3288 制作内核开机logo
安装工具 sudo apt-get install netpbm 1.制作图片 (1).图片为bmp格式 $ convert logo.bmp logo.png $ pngtopnm logo.png ...
- 将h264和aac码流合成flv文件
在视频应用中,经常需要将接收到h264和aac数据保存成文件. 本来想用mp4格式,但是mp4在没有正常关闭的情况下会导致文件打不开,而在实际应用中经常会出现设备直接拔电,程序不是正常结束的情况.于是 ...
- Java-Runoob-高级教程: Java 多线程编程
ylbtech-Java-Runoob-高级教程: Java 多线程编程 1.返回顶部 1. Java 多线程编程 Java 给多线程编程提供了内置的支持. 一条线程指的是进程中一个单一顺序的控制流, ...
- 这段时间使用MySQL的一些记录
自从Fedora19之后,Linux上的MySQL就被MariaDB所取代,这段文字见如下引用: MySQL was replaced by MariaDB since Fedora 19 (http ...
- Hive使用入门
Hive简介 hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运行. 其优点是学习成 ...
- php网站环境无法上传的解决办法?
一. 检查网站目录的权限.二. php.ini配置文件php.ini中影响上传的有以下几处:file_uploads 是否开启 on 必须开启是否允许HTTP文件上传post_max_size = 8 ...