[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 ...
随机推荐
- CComPtr用法
COM接口指针很危险,因为使用过程中需要每一个使用者都要严格并且正确的AddRef和Release,一旦出现问题,就会造成对象不能被正常释放,或者对象被重复删除,造成程序崩溃.所以使用COM接口,必须 ...
- [Android Webkit]JNI基础及Java层与C++层的交互
1. JNI 注册 1.1. JNI的基础结构 JAVA == JNI == Native Code JNI(Java Native Interface)是Java与Native ...
- android如何让service不被杀死
1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建 @Override public int onStartCom ...
- 逆拓扑排序 HDU2647Reward
这个题如果用邻接矩阵的话,由于n比较大,会超内存,所以选用邻接表的形式.还有就是这个题有那个等级的问题,一级比一级的福利高,所以不能直接拓扑排序,而是反过来,计算出度,找出度为0的顶点,然后更新出度数 ...
- SpringMVC09异常处理和类型转化器
public class User { private String name; private Integer age; public String getName() { return name; ...
- (转)SQL流程控制语句学习(一):变量及控制语句种类
1.局部变量 用户自己定义的,称局部变量,以@标识. 作用范围:定义局部变量的批处理.存储过程.触发器和语句块 局部变量的定义: declare @局部变量名 数据类型 注意:变量的类型不能是text ...
- iOS 网络与多线程--1.检测网络链接状态
通过Reachability库,检测设备的网络连接状况. 使用到的类库:Reachability Reachability库,是一个iOS环境下,检测设备网络状态的库,可以在网络上搜索下载. 使用之前 ...
- 【转】Understanding and Using rem Units in CSS
CSS units have been the subject of several articles here on SitePoint (such as A Look at Length Unit ...
- Knockoutjs官网翻译系列(四) computed中依赖追踪是如何工作的
初学者无需了解这些 ,但是很多高级程序员想知道我们为什么可以保持跟踪这些依赖以及可以正确的更新到UI中.它其实很简单.跟踪算法是这样的: 无论何时你定义了一个computed observable,K ...
- jQuery自学笔记(一):初识jQuery
jQuery 是一个 JavaScript 函数库, jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数,引用jQuery应该注意: <script&g ...