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的更多相关文章

  1. 【leetcode】1027. Longest Arithmetic Sequence

    题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...

  2. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  3. [LeetCode] 128. Longest Consecutive Sequence 解题思路

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  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(leetcode)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  8. [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 ...

  9. Java for LeetCode 128 Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

随机推荐

  1. ransom-note

    https://leetcode.com/problems/ransom-note/ public class Solution { public boolean canConstruct(Strin ...

  2. jquery获取元素各种宽高及页面宽高总结

    window.onload=function(){ var a = $("#div").width(),//width()返回元素的宽高,不包括padding/border/mar ...

  3. Volley框架的介绍使用

    Volley是在2013年的Google I/O 2013大会上发布的,是我们的网络通信更快,更简单,更方便.对于初学者来讲是一个很好的框架. 简单来说,它提供了如下的便利功能: JSON,图像等的异 ...

  4. Clion安装配置

    1.安装配置MinGW MinGW安装和使用 Windows下利用Cygwin搭建C/C++开发环境GCC 注:分别是使用MinGW和Cygwin两种方法安装GCC环境,据说Cygwin会好一点……我 ...

  5. PU-bound(计算密集型) 和I/O bound(I/O密集型)

    转载:https://blog.csdn.net/q_l_s/article/details/51538039 I/O密集型 (CPU-bound) I/O bound 指的是系统的CPU效能相对硬盘 ...

  6. Android实现固定头部信息,挤压动画(相似通讯录)

    半年前,那时候我还是个大四的学生,每天都在找工作度过,想去北京体验一下蚁族生活,奋然离开了济南,哎...在济南我们学校还是数得着的好学校,去了北京就什么都不是了,一切的辛酸仅仅有自己知道,那时候的我仅 ...

  7. 如何使用SubtitleWorkshop制作字幕

    任意打开一段字幕文件 对于初学者而言还是最好打开一个带有中英字幕的视频文件(字幕是嵌入在视频文件里面的)然后一句一句照着写 先打开视频预览模式 再打开一段视频文件,并新建一个字幕文件 牢记几个快捷键 ...

  8. .NET破解之迅捷PDF转换器(续)

    在以前的博文<.NET破解之迅捷PDF转换器>中使用了暴力破解的方法,现在软件版本从5.0升级到6.3,所以也尝试用新的方法. 方法一:暴力破解法 如往常一样,查找搜索到关键的函数,即Is ...

  9. ArcEngine开发各种几何错误代码

    E_GEOMETRY_AMBIGUOUSPARTTYPE - Static variable in interface com.esri.arcgis.geometry.esriGeometryErr ...

  10. awk的使用

    http://www.cnblogs.com/chengmo/archive/2010/10/08/1845913.html linux awk 内置函数详细介绍(实例) awk内置字符串函数 awk ...