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. JZOJ 5934. 列队

    Description         Sylvia是一个热爱学习的女孩子.        在平时的练习中,他总是能考到std以上的成绩,前段时间,他参加了一场练习赛,众所周知,机房是一个 的方阵.这 ...

  2. Linux基础(02)、MTPutty安装和使用

    准备工具 1. MTPutty的安装包 2. Putty.exe程序 作用:远程连接操作Centos 安装MTPutty 1.根据提示,一直下一步至下图:选择putty.exe文件的位置即可. 2.选 ...

  3. Python系列8之socket

    目录 socket 简单的聊天机器人 简单的ftp上传 粘包问题的解决 一. socket模块 socket,俗称套接字,其实就是一个ip地址和端口的组合.类似于这样的形式(ip,  port),其中 ...

  4. ADB工具的安装

    1.Windows ADB工具下载地址: https://developer.android.google.cn/studio/releases/platform-tools ADB工具官网教程: h ...

  5. ABAP CDS ON HANA-(8)算術式

    Arithmetic expression in CDS View Allowed Arithmetic operators in CDS view. CDS View- @AbapCatalog.s ...

  6. Xshell入门教程介绍

    免费软件 Xshell和 Xftp 都是 NetSarang 出品的优秀网络管理.安全传输工具.Xshell 是一个免费的安全终端仿真器,可以作为 SSH.TELNET 或 RLOGIN 的终端模拟, ...

  7. WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法

    原文:WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法 问题描述 今天发现如果我想在一个TextBlock弄一个右键菜单,并且使用Command绑定,结果发 ...

  8. vi编辑图

    vi使用方法

  9. 步骤1:JMeter 录制脚本接口测试

    JMeter 常用测试方法简介 1.下载安装 http://jmeter.apache.org/download_jmeter.cgi 安装JDK,配置环境变量JAVA_HOME. 系统要求:JMet ...

  10. 序列化反序列化--Xstream的使用

    之前讲了fastjson的使用--将JavaBean与json对象之间互相转换. 该篇文章,教大家使用Xstream来实现XMl与JavaBean的转换. 第一步: 通过maven引入XStream的 ...