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

要求算法的时间复杂度为 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. 运算符 Operator 及优先级

    算数运算符 + - * / ** % /表示自然除,结果是浮点数.//为整除.python2.x版本/和//都是整除. 位运算符 & | ~ ^ << >> <& ...

  2. 5.8 Nginx 常用功能的配置

  3. 留学Essay写作:精准用词很重要

    很多觉得自己英语成绩还行的同学经常在自己的文章里用一些浮夸或者是难度特别大的词语,以显示自己的英语水平.当然了,成功了倒也无可厚非,要是有些词语用错了那可是会被别人笑掉大牙的.因此英语中的精准用词就成 ...

  4. centos 6.5安装NodeJS

    centos 6.5安装NodeJS 下载 可以在本地下载node.js最新版,然后通过ftp工具上传到服务器,或者直接在服务器终端使用wget命令下载(我当时下载的是node-v7.5.0-linu ...

  5. SciPy 教程

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  6. 微软重制Windows 1.0系统:祖师爷出山了

    Windows官方推特在7月1日发布了一条很有趣的动态,“向大家介绍全新的Windows 1.0,带MS-DOS.时钟等”.配发的视频回顾了从Windows 1.0/3.1到Windows 10期间, ...

  7. PHP-WebShell-Bypass-WAF

    PHP-WebShell-Bypass-WAF PHP WebShell 一句话的结构是:输入和执行,这是经典的PHP 一句话代码: <?php eval($_GET['test']); ?&g ...

  8. es和数据类型

    js=es+dom+bom,dom和bom前面已经讲完了 es是js的本体,是指数据类型,和对于数据的操作手段,他的版本更新得很快 这些功能不是html文件提供的,也不是浏览器提供的,即使脱离了dom ...

  9. IDEA中利用MAVEN制作和打包普通可执行应用(非SprintBoot的WEB应用)

    我使用IDEA也就半年,开发中常常会遇到一些问题,例如用IDEA编写普通的可执行程序: 之前使用Eclipse编写一个可执行的JAVA程序,然后导出打包,非常方便: 但是我呢,想在 IDEA 中用Ma ...

  10. 038、Java中逻辑运算之非运算“!”

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