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. pyspider -- 禁止请求非200响应码抛异常

    在pyspider中若crawl()网址时出现非200的异常信息,会抛出一个异常. 可以在对应的回调函数上面通过@catch_status_code_error 进行修饰,这样就能不抛出异常正常进入回 ...

  2. makefile = 与 := 的区别

    “=” make会将整个makefile展开后,再决定变量的值.也就是说,变量的值将会是整个makefile中最后被指定的值.看例子: x = foo            y = $(x) bar  ...

  3. 004---os & sys

    os模块和sys模块 这两个模块都提供了很多与操作系统之间交互的功能 使用 import os #当前脚本的工作目录,不是脚本目录 print(os.getcwd()) # 获取指定目录下的所有文件和 ...

  4. python基础之socket套接字基础part2

    基于UDP的socket 面向无连接的不可靠数据传输,可以没有服务器端,只不过没有服务器端,发送的数据会被直接丢弃,并不能到达服务器端 1 #客户端 2 import socket 3 ip_port ...

  5. Django学习之天气调查实例(1):工程的开始

    开始学习Django,一步一个脚印的进行.思考再三,还是以一个实例来开始学习.手里面正好有几万条单位天气传感器收集的数据,想做一个网页版的天气统计查询之类的小应用,也可以给学生体验,方便教学的进行(尽 ...

  6. c++ combination by next_permutation

    #include <iostream> #include <algorithm> #include <vector> int main() { int n, r; ...

  7. Prolog奇怪奇妙的思考方式

    今天在<七周七语言>中接触到了prolog,发现它的编程模式和思考方式的确比较奇怪,但同时也非常奇妙,值得学习一下. 1. prolog语言介绍     和SQL一样,Prolog基于数据 ...

  8. 子串查询(二维前缀数组) 2018"百度之星"程序设计大赛 - 资格赛

    子串查询 Time Limit: 3500/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Subm ...

  9. npx 命令介绍

    这个是在 npmv5.2.0引入的一条命令(查看),引入这个命令的目的是为了提升开发者使用包内提供的命令行工具的体验. 为什么引入这个命令 举个例子,我们开发中要运行 parcel 命令来打包:par ...

  10. nginx初探,下载安装配置负载均衡

    上一篇我讲了正向代理和反向代理的概念,这个是为nginx做准备的前置技能,网上百度nginx可以知道nginx是什么: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/PO ...