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. 信号处理篇alarm ferror kill mkfifo pause pclose perror pipe popen sigaction sigaddset sigdelset sigemptyset signal sleep strerror

    alarm(设置信号传送闹钟) 相关函数 signal,sleep 表头文件 #include<unistd.h> 定义函数 unsigned int alarm(unsigned int ...

  2. MyBatis Lazy Loading

    MyBatis的Lazy Loading可以实现延迟查询Bean里的嵌套成员类,控制lazy loading的<settings>属性有 lazyLoadingEnabled: lazy ...

  3. Java多媒体编程应用

    1. 声音文件的播放 1.1 在Applet中播放声音 在Applet中,可以使用AudioClip来播放声音,它非常简单,只有三个方法:play().loop()和stop(). 例1.1 利用Au ...

  4. iOS: 音效和音乐的播放,封装的工具类

    在iOS中音频播放从形式上可以分为音效播放和音乐播放.前者主要指的是一些短音频播放,通常作为点缀音频,对于这类音频不需要进行进度.循环等控制.后者指的是一些较长的音频,通常是主音频,对于这些音频的播放 ...

  5. opencv2.4中SVD分解的几种调用方法

    原帖地址: http://blog.sina.com.cn/s/blog_6109b5d00101ag7a.html       在摄影测量和计算机视觉中,考虑最优解问题时,经常要用到SVD分解.奇异 ...

  6. 我也用github(2)——关联本地工程到github仓库

    github只是为我们提供了一个存储的功能,我们也可以准备一个服务器(当然,能联网是前提了),将版本库保存到服务器上. 本文以github为例进行实验. 1. 在github上创建一个仓库 这个非常简 ...

  7. Informatica 常用组件Source Qualifier之七 使用排序端口

    使用已排序端口时,PowerCenter 将添加端口至默认查询中的 ORDER BY 子句.PowerCenter Server 将添加配置的端口号,从源限定符转换的顶部开始.在映射中包括以下任何转换 ...

  8. IDA远程调试so库JNI_Onload函数

    JNI_OnLoad函数大概功能就是在程序加载so的时候,会执行JNI_OnLoad函数,做一系列的准备工作.很多时候,程序猿们会将一些重要信息放在此函数中,而不是通过某种事件来重复触发.包括说将反调 ...

  9. BFC是什么

    BFC 已经是一个耳听熟闻的词语了,网上有许多关于 BFC 的文章,介绍了如何触发 BFC 以及 BFC 的一些用处(如清浮动,防止 margin 重叠等).虽然我知道如何利用 BFC 解决这些问题, ...

  10. 防盗链Nginx设置图片防盗链,设置无效的请仔细看红字

    *******************************************************************切记,替换的图片地址要使用没有防盗链的网站图片,否则由于替换的图片 ...