【LeetCode】128. Longest Consecutive Sequence
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.
想法一:
用01数组代表该数字是否存在序列中,化归为:一个01序列中,最长连续1的问题。
小数据上可以,但是大数据测试上遇到了问题,当数据跨度很大,比如INT_MIN~INT_MAX时,空间复杂度太高。
想法二:
用map代替数组实现上面的想法,可以解决空间复杂度问题。
但是在计算最长连续1的过程中发现新的问题,遍历INT_MIN~INT_MAX的时间复杂度太高。
解法:
对num数组中的元素进行左右最大扩展计数,代替全范围的遍历计数。
class Solution {
public:
int longestConsecutive(vector<int> &num) {
int ret = ;
unordered_map<int, bool> m;
for(int i = ; i < num.size(); i ++)
m[num[i]] = false;
for(int i = ; i < num.size(); i ++)
{
if(m[num[i]] == false)
{
int cur = ;
int left = num[i]-;
while(m.find(left) != m.end() && m[left] == false && left >= INT_MIN)
{
m[left] = true;
cur ++;
left --;
}
int right = num[i]+;
while(m.find(right) != m.end() && m[right] == false && right <= INT_MAX)
{
m[right] = true;
cur ++;
right ++;
}
ret = max(ret, cur);
}
}
return ret;
}
};

【LeetCode】128. Longest Consecutive Sequence的更多相关文章
- 【leetcode】1027. Longest Arithmetic Sequence
题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- [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. F ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- MIR Flickr 1M 图像数据集(点击即可下载)
Index of /mirflickr/mirflickr1m Name Last modified Size Description Parent Directory - exif.zip ...
- python各个模块循环引用问题解决办法
当项目中的模块过多,或功能划分不够清晰时会出现循环引用的问题,如下 有两个模块moduleA 和 moduleB: #moduleA from moduleB import b def a(): pr ...
- HBase性能调优(转)
原文链接:http://www.blogjava.net/ivanwan/archive/2011/06/15/352350.html 因官方Book Performance Tuning部分章节没有 ...
- perfect-rectangle
https://leetcode.com/problems/perfect-rectangle/ // https://discuss.leetcode.com/topic/55944/o-n-log ...
- 【软引用】弱引用 图片的加载与缓存 OOM
在java.lang.ref包中提供了几个类:SoftReference类.WeakReference类和PhantomReference类,它们分别代表软引用.弱引用和虚引用. ReferenceQ ...
- TreeListControl 不同类别的行使用不同的数据模板
- Win10系统下软件UI显示不完整解决方案
在最初升级win10的时候就想到了这些问题,例如和各种软件的不兼容性.当然,事实上win10并没有想象的那么糟,作为一个windows user 来说,win10的确是很高大上的,无论是颜值或者是体验 ...
- cygwin64安装wget和apt-cyg
说实话,网上的教程要么不适用,要么不能用,唯有多次试错之后才杀出一条血路. 1.安装cygwin 2.勾选wget 这个如果忘记勾选了,就再次打开setup.exe,选择如下操作,即可. 然后点击下一 ...
- [Functional Programming 101] runWIth, evalWith, execWith
Recentlly, I am learning crocks.js ADT libaray. In the beginning, it is hard to understand when to u ...
- 分享几套古典复古式的UI设计
古典复古的UI界面设计能够非常好的点缀你的网页,特别是如果你需要展示你的产品或者内容的深度内涵或者体验你的内容或者产品的经久不衰的气质的 话,古典或者复古式的界面UI绝对是你首选,在这里我们收集了10 ...