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

要求算法的时间复杂度为 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. CMake查找第三方库路径

    问题 一直都有一个问题,就是基于Windows下使用CMake构建VS工程时,CMake是如何查找到第三方库所在的路径的呢? 答案 今天重新想起这个问题,就拿构建Vtk的VS工程测试了一下, 才发现是 ...

  2. php 实现店铺装修3

    /** * @title 装修店铺 * @param plate_id 是 int 店铺装修样式ID * @param type 是 int 模板样式子板块类型(4-海景.6-二宫格.8-三宫格.9- ...

  3. 068、Java面向对象之声明两个对象

    01.代码如下: package TIANPAN; class Book { // 定义一个新的类 String title; // 书的名字 double price; // 书的价格 public ...

  4. ObjectMapper : can only instantiate non-static inner class by using default, no-argument constructor

    Label_t lTrain = new ObjectMapper().readValue(s, Label_t.class); 因为Label_t是内部类,需要 1.static 2.无参构造函数

  5. hdu 1533 Going Home 最小费用最大流 (模板题)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. greenplum 存储过程 返回结果集多列和单列

    参考: http://francs3.blog.163.com/blog/static/4057672720125231223786/

  7. (实例)Linux 内核添加exfat驱动

    背景: 由于exfat是常用的文件系统格式,而Linux由于版权的问题,没有在官方中添加有关的驱动. 但是 微软也同意开源了,所以比较新的 Linux 会支持这一块. 为了支持exfat的驱动,我们需 ...

  8. mac安装扩展出现grep: /usr/include/php/main/php.h

    在Mac下执行 sudo phpize时提示: grep: /usr/include/php/main/php.h: No such file or directorygrep: /usr/inclu ...

  9. HiBench成长笔记——(9) 分析源码monitor.py

    monitor.py 是主监控程序,将监控数据写入日志,并统计监控数据生成HTML统计展示页面: #!/usr/bin/env python2 # Licensed to the Apache Sof ...

  10. docker 为镜像添加ssh服务-docker commit命令创建

    环境centos7 一.准备工作 docker pull ubuntu:18.04 docker run -it ubuntu:18.04 bash 二.配置软件源apt-get update,如果系 ...