【问题】给定一个未排序的整数数组,找出最长连续序列的长度。

要求算法的时间复杂度为 O(n)。

示例:

输入: [, , , , , ]
输出:
解释: 最长连续序列是 [, , , ]。它的长度为 。

【思路】

首先使用一个哈希set将我们的数据全都保存,然后遍历整个数组,假如遍历到了数字A,其一定在哈希set中,这是毋庸置疑的。接着我们需要进入一个while循环去判断A+1,A+2,A+3…是不是也在这个哈希表中,如果尝试到数字B,其不在哈希表中则退出,此时最长连续序列B-A。

既然我们查找过了A+1,A+2,A+3, 其在哈希表中,那么我们遍历数组的时候要需要遍历这些值么?显然不需要,因此我们可以优化一下,什么时候才需要进入上述过程!

当某一个数的前一个数没有在数组,也就是没有在哈希set中,我们就从这个数字开始遍历,统计到的连续序列一定是该部分最长的!!!

class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if(nums.size() == ) return ;
unordered_set<int> mySet(nums.begin(), nums.end());
int res = ; for(auto num: nums){
if(mySet.count(num - ) == ){
int tmp = num; //保存初始数据
while(mySet.count(tmp)){
tmp++;
}
res = max(res, tmp-num);
}
}
return res;
}
};

【LeetCode】最长连续序列的更多相关文章

  1. Leetcode 最长连续序列

    题目链接:https://leetcode-cn.com/problems/longest-consecutive-sequence/ 题目大意: 略. 分析: 注意有重复值,序列为空等情况. 代码如 ...

  2. leetcode 最长连续序列 longest consecutive sequence

    转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 2 ...

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

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

  4. [LeetCode] Longest Consecutive Sequence 求最长连续序列

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

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

  6. 图解leetcode —— 128. 最长连续序列

    前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). ...

  7. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

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

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

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

  9. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  10. [leetcode]128. Longest Consecutive Sequence最长连续序列

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

随机推荐

  1. 「PA2014」Fiolki

    传送门 Bzoj 解题思路 构造法. 对于每一次的倾倒操作,连边 \(newnode\to u,newnode\to v\). 最后所有的反应都会在构造出来的树上的对应两点的 \(\text{LCA} ...

  2. spingboot中使用scheduled设置定时任务注意事项

    在spring开发过程中经常会遇到需要定时执行的任务,如定时生成报表,定时推送消息等任务. springboot 提供了简单的 @Scheduled 表达式来配置定时任务.该方式默认是单线程的,任务在 ...

  3. 106、Java中String类之使用contains()方法判断子字符串是否存在

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  4. IPython 自动重载魔术

    在开启IPython 后输入下列命令就可以开启Ipython 的自动重载 %load_ext autoreload %autoreload 2 当你在IPython中导入的函数或类发生修改时,IPyt ...

  5. qrcode在手机上不显示的问题

    可以试试以下解决方案: 1.修改qrcode.min.js:里的function n()红线区域替换成这个 , 原因是这样子才能支持安卓机显示.

  6. 多元线性回归算法的python底层代码编写实现

    1.对于多元线性回归算法,它对于数据集具有较好的可解释性,我们可以对比不过特征参数的输出系数的大小来判断它对数据的影响权重,进而对其中隐含的参数进行扩展和收集,提高整体训练数据的准确性. 2.多元回归 ...

  7. robot_framework + selenium + 上传本地文件+win7 32位

    1.下载与安装AutoIt v3  地址链接:http://pan.baidu.com/s/1hqsDFBA,我自己是32位的系统,用这个运行可以 2.安装完成后,如下图所示 3. AutoIt Wi ...

  8. Windows下使用nginx问题

    1.下载完成后,解压缩,运行cmd,使用命令进行操作,不要直接双击nginx.exe,不要直接双击nginx.exe,不要直接双击nginx.exe 一定要在dos窗口启动,不要直接双击nginx.e ...

  9. POJ 3349:Snowflake Snow Snowflakes 六片雪花找相同的 哈希

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 35642   Accep ...

  10. bugku love

    emmm....控制台运行一下 接着查一下是否有壳 显示是没有壳的,接着查看一下结构 也没有什么发现,上ida看一下吧,顺便说一句每个人的解题思路都不一样.面对一开始都不清楚的结构我会选择交叉引用这样 ...