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(n),所以不行!这题可以使用到关联容器的知识(见博客关联容器详解(一))。然后就涉及选择容器的问题了,是选择有序的set,还是无序的unordered_set(当然可以使用map类的)?因为set会直接给元素进行排序,其原理涉及平衡搜索二叉树(即红黑树),时间复杂度不满足。所以这里利用unordered_set。大致的思路是:从头到尾遍历数组,若某数在set中,则遍历其左右两边的数(注意,这里是数组存放的值不是下标),并删除该数。找到目前的最大长度,在继续访问数组中的元素。代码如下:

 class Solution {
public:
int longestConsecutive(vector<int> &num)
{
int res=;
unordered_set<int> s(num.begin(),num.end()); for(int i=;i<num.size();++i)
{
if(s.count(num[i]))
{
s.erase(num[i]);
int pre=num[i]-,next=num[i]+;
while(s.count(pre))
s.erase(pre--);
while(s.count(next))
s.erase(next++); res=max(res,next-pre-);
}
}
return res;
}
};

关于unordered_map的使用方法可以参见Grandyang的博客。

[Leetcode] Longest consecutive sequence 最长连续序列的更多相关文章

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

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

  2. 298. Binary Tree Longest Consecutive Sequence最长连续序列

    [抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...

  3. 128. Longest Consecutive Sequence最长连续序列

    [抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  4. LeetCode——Longest Consecutive Sequence

    LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...

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

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

  6. LeetCode: Longest Consecutive Sequence 解题报告

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  7. [Leetcode] Longest Consecutive Sequence 略详细 (Java)

    题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...

  8. LeetCode: Longest Consecutive Sequence [128]

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

  9. [LeetCode] Longest Consecutive Sequence

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

随机推荐

  1. jdk与tomcat的环境配置

    一.JDK的安装与配置 1.从官网下载jdk,注意是jdk不是jre.最好从官网下载,也可以直接度娘. 2.下载完毕后,安装jdk,​直接按照安装向导的提示安装即可,安装时可以自己选择安装路径,我的安 ...

  2. python 装饰器 回顾 及练习

    # 复习 # 讲作业 # 装饰器的进阶 # functools.wraps # 带参数的装饰器 # 多个装饰器装饰同一个函数 # 周末的作业 # 文件操作 # 字符串处理 # 输入输出 # 流程控制 ...

  3. 文件 I/O缓冲流

    import java.io.File; import java.io.Writer; import java.util.StringTokenizer; import java.io.Reader; ...

  4. R语言学习笔记(八):零碎知识点(16-20)

    16--complete.cases( ) complete.case()可以判断对象中是否数据完全,然后返回TRUE, FALSE 这一函数在去除数据框中缺失值时很有用. > d kids a ...

  5. HDU1209:Clock

    参考:https://blog.csdn.net/libin56842/article/details/8990530 https://blog.csdn.net/u011479875/article ...

  6. 【转】mui 通过JSON动态的生成列表

    <script type="text/template" id="radio-tigan"> <%for(var i=0;i<recor ...

  7. MySQL server has gone away 错误处理

    解决方案1: 这个是mysql自身的一个机制:     mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决办法有两个:     1.修改mysql 配置               增 ...

  8. 使用apache的ab压力测试时失败请求原因

    只要出现 Failed requests 就会多出现一行要求失败的各原因的数据统计,分别有 Connect, Length,与 Exception 三种,分别代表的意义为:Connect      无 ...

  9. 【APUE】Chapter4 File and Directories

    4.1 Introduction unix的文件.目录都被当成文件来看待(vi也可以编辑目录):我猜这样把一起内容都当成文件的原因是便于统一管理权限这类的内容 4.2 stat, fstat, fst ...

  10. 【个人训练】(POJ1276)Cash Machine

    最近的很多题解应该都是dp相关的了,emmm因为dp对我而言思考难度比较大,那么为了理顺自己的思路当然只能通过写blog整理了.愿我能成功搞定dp这个大关!(至少中等难度的dp要能够解决啊o(TヘTo ...