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

要求算法的时间复杂度为 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. spinner的使用

    item.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  2. 输入、输出(iostream)

    在一个程序当中输入和输出都扮演着重要的角色,所以掌握基本输入输出是入门一门语言所必不可少的.本文主要简单叙述java的输入和输出. package ios; import java.util.Scan ...

  3. HDU-1312题解(DFS)

    HDU-1312-DFS Written by Void-Walker    2020-02-01 09:09:25 1.题目传送门:http://acm.hdu.edu.cn/showproblem ...

  4. Logback的AsyncAppender与RollingFileAppender流程解析

    近期工作中涉及到文件记录.文件翻转等操作,思考有没有成熟的代码以便参考. 因此,第一时间就联想到Logback的AsyncAppender以及RollingFileAppender. AsyncApp ...

  5. 51nod 1391:01串

    1391 01串 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 给定一个01串S,求出它的一个尽可能长的子串S[i. ...

  6. Easy_Re

    这题比较简单,一波常规的操作之后直接上ida(小白的常规操作在以前的博客里都有所以这里不在赘述了),ida打开之后查看一下, 这里应该就是一个入口点了,接着搜索flag字符串, 上面的黄色的部分转换成 ...

  7. 基于LAMP实现后台活动发布和前端扫码签到系统

    目的 无论是公司.学校和社会团体,都会举办各式各样的活动,比如运动会.部门会议.项目会议.野炊.团建等.作为团队管理者来讲,当然希望能够把这类活动转移到线上形成完整的系统,类似于电子流的形式.本文以学 ...

  8. Metasploit学习笔记——Web应用渗透技术

    1.命令注入实例分析 对定V公司网站博客系统扫描可以发现,它们安装了zingiri-web-shop这个含有命令注入漏洞的插件,到www.exploit-db.com搜索,可以看到2011.11.13 ...

  9. python matplotlib绘图大全(散点图、柱状图、饼图、极坐标图、热量图、三维图以及热图)

    //2019.7.14晚matplotlib七种常见图像输出编程大全 七种图形汇总输出如下: import numpy as np #导入数据结构nmupy模块import matplotlib.py ...

  10. 02 DML(DataManipulationLanguage)

    1.插入记录     基本语法 :         INSERT INTO tbl_name (col_name ,col_name1,..,col_nameN) VALUES (val1,val2, ...