[LeetCode] 128. 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.
问题:给定一个无序数组,找出最长的连续序列,要求时间复杂度为 O(n) 。
一开始想到的是用先排序,再找结果,但是时间复杂度要求 O(n) ,使用排序会超时。
思索未果,再网上找到一个方案,借助 unordered_set 来实现。
将元素全部塞进 unordered_set 中。
取出 unordered_set 中的一个剩余元素 x ,找到 unordered_set 中 x 的全部前后相邻元素,并将 x 和相邻元素全部移除,此时能得到 x 的相邻长度。若 unordered_set 还有元素,则继续当前步骤。
在第二步中的所有相邻长度中,找出最大值便是原问题的解。
由于 unordered_set 是基于 hash_table 来实现的,所以每次插入、查找、删除都是 O(1),而全部元素只会 插入、查找、删除 1 次,所以整体复杂度是 O(n)。
int longestConsecutive(vector<int>& nums) {
unordered_set<int> theSet;
for (int i = ; i < nums.size(); i++) {
theSet.insert(nums[i]);
}
int longest = ;
while (theSet.size() > ) {
int tmp = *theSet.begin();
theSet.erase(tmp);
int cnt = ;
int tmpR = tmp + ;
while (theSet.count(tmpR)) {
cnt++;
theSet.erase(tmpR);
tmpR++;
}
int tmpL = tmp - ;
while (theSet.count(tmpL)) {
cnt++;
theSet.erase(tmpL);
tmpL--;
}
longest = max( longest, cnt);
}
return longest;
}
参考资料:
[LeetCode] Longest Consecutive Sequence, 喜刷刷
[LeetCode] 128. Longest Consecutive Sequence 解题思路的更多相关文章
- [LeetCode] 128. 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]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Leetcode 128. Longest Consecutive Sequence (union find)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
- Leetcode#128 Longest Consecutive Sequence
原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...
- 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
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
随机推荐
- cocos2d-x 2.2.3 之菜单分析(1)
TextEdit-Menu CCtextFieldTTF cocos2d – x 中提供的 bool T04ZORDER::init() { if (!CCLayer::init()) { retur ...
- lvs keepalived 安装配置详解【转】
lvs keepalived 安装配置详解 张映 发表于 2012-06-20 分类目录: 服务器相关 前段时间看了一篇文章,lvs做负载均衡根F5差不多,说实话不怎么相信,因为F5没玩过,也无法比较 ...
- Hibernate的fetch
hibernate抓取策略fetch具体解释一.hibernate抓取策略(单端代理的批量抓取fetch=select(默认)/join)測试用例:Student student = (Student ...
- Android应用程序之间共享文字和图片(一)
以下为TestReceiveShare1工程 MainActivity如下: package cn.testreceiveshare1; import java.util.ArrayList; imp ...
- Unix系统解压tar包时出现@LongLink错误
Unix系统上使用tar命令解压tar包后,多了一个@LongLink的文件,并且原来的tar包解压后不完整.网上查了下,原因是AIX系统上tar命令自身的一个缺陷.解决办法:把该tar包上传到lin ...
- python对象(腌制)
python的内置对象类型主要有数字,字符串,列表,元祖,字典,集合等等,在python中,一切皆为对象 #腌制在python中如果我们有一些对象需要持久性存储,并且不丢失我们这个对象的类型与数据,我 ...
- C#总结项目《影院售票系统》编写总结三
昨天总结了动态绘制控件.票类型的切换以及数据在窗体中的展现.今天继续总结,自己喜欢的就去做吧,让别人说去吧,省的自己再留下什么后悔遗憾,噢耶,加油! 今天总结项目中最核心的部分--购票.座位颜色状态的 ...
- 2015-09-28 Javascript
1.Javascript是什么? JavaScript是一种脚本语言,结构简单,使用方便,其代码可以直接放入HTML文档中,可以直接在支持JavaScript的浏览器中运行.JavaSript. Ja ...
- MySQL性能调优与架构设计读书笔记
可扩展性设计之数据切分 14.2 数据的垂直切分 如何切分,切分到什么样的程度,是一个比较考验人的难题.只能在实际的应用场景中通过平衡各方面的成本和利益,才能分析出一个真正适合自己的拆分方案. 14. ...
- php单例模式深入讲解
避免多次初始化数据库连接DAO 需要多次初始化数据库连接的场景 场景1: 首先PHP单例模式我觉得只是针对单次页面级请求时出现多个应用场景并需要共享同一对象资源时是非常有意义的 一个类A需要调用多个类 ...