描述

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.

分析

对于这类题目,我们最自然的想法就是先排序,再对每一个数据进行处理。但是最常用的排序算法快速排序的平均运行时间也有O(nlgn),插入排序在最好的情况下才能达到O(n)的复杂度。

因此,这题不能用“先排序,再处理”的思路,可以考虑使用哈希表unordered_map<int,bool>

该map记录每个元素是否被使用,对每个元素,以该元素为中心,分别向两边扩张,并记录扩张的长度,直到不连续时为止。

如:考察数组 [100, 4, 200, 1, 3, 2],从第一个元素(100)开始遍历每一个元素,分别查找向左(减一)和向右(加一)后的元素是否在该数组中,若在数组中,则将长度加一,同时将bool值置为true,以表示“该元素已和数组中的另一元素连续,不必再对该元素进行处理”。否则,退出循环。

这样,我们就能得到每一个元素的最大长度,只要在每一次遍历后更新最大长度就可以了。

代码如下:

class Solution{
public:
int longestConsecutive(const vector<int>&nums){
int longest = 0; //record the longest consecutive
unordered_map<int,bool>used;
for(auto num : nums){
int length = 1;
used[num] = true;
for(int next = num + 1; used.find(next) != used.end(); ++next){
used[next] = true;
++length;
} for(int pre = num - 1; used.find(pre) != used.end(); --pre){
used[pre] = true;
++length;
} longest = max(longest,length);
}
return longest;
} };

leetcode解题报告(5):Longest Consecutive Sequence的更多相关文章

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

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

  3. LeetCode 549. Binary Tree Longest Consecutive Sequence II

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...

  4. LeetCode 298. Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  5. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

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

  6. LeetCode(128) Longest Consecutive Sequence

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

  7. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  8. leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解

    dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...

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

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

  10. [LintCode] 619 Binary Tree Longest Consecutive Sequence III 二叉树最长连续序列 III

    Given a k-ary tree, find the length of the longest consecutive sequence path. The path could be star ...

随机推荐

  1. tomcat中server.xml各项配置详解

     详见大佬的文章,乐于做大佬文章的搬运工. http://www.cnblogs.com/starhu/p/5599773.html

  2. EXIT(外部中断)控制实验

    实验目的 设计使用外接的按键来作为触发源,使得控制器产生中断,并在中断服务函数中实现控制小灯的亮灭. 按键硬件点路 编程要点 初始化用来产生中断的 GPIO: 初始化 EXTI: 配置 NVIC: 编 ...

  3. 2019java学习路线图

    学习路线图往往是学习一样技术的入门指南.网上搜到的Java学习路线图也是一抓一大把.但是很多学习路线图总结的云里雾里,也没有配套的视频,学习效果并不好. 分享一个完整的Java学习路线图给大家,也是贴 ...

  4. (六)Redis之数据结构之sorted-set

    一.常用方法 Sorted-Set和Set的区别 Sorted-Set中的成员在集合中的位置是有序的 添加元素 获得元素 删除元素 范围查询 1和2和3和4 添加/获得/删除元素/范围查询 packa ...

  5. 数据库及MYSQL基础(3)-JDBC

    教学视频链接:https://edu.aliyun.com/course/1694?spm=5176.11400004.0.0.29254768sg2H5P 程序文件链接:https://pan.ba ...

  6. $.ajax()属性详解

    $.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Strin ...

  7. nginx增加新模块

    以gunzip这个模块为例,讲述一下,在nginx中如何安装新的模块 1.首先查看nginx已经安装了哪些模块. nginx –V 2.发现没有gunzip模块,安装 进入nginx的安装目录中,不是 ...

  8. Signal Processing and Pattern Recognition in Vision_15_RANSAC:Random Sample Consensus——1981

    此部分是 计算机视觉中的信号处理与模式识别 与其说是讲述,不如说是一些经典文章的罗列以及自己的简单点评.与前一个版本不同的是,这次把所有的文章按类别归了类,并且增加了很多文献.分类的时候并没有按照传统 ...

  9. CentOS 8 (1905)系统安装

    本章内容: CentOS 8 的安装(CentOS-8-1905) 一.安装光盘,选择Install CentOS Linux 8.0.1905 二.选择系统语言,我这里选的是英文,也可以选择中文,往 ...

  10. Django drf:序列化增删改查、局部与全局钩子源码流程、认证源码分析、执行流程

    一.序列化类的增.删.改.查 用drf的序列化组件   -定义一个类继承class BookSerializer(serializers.Serializer):   -写字段,如果不指定source ...