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.

问题

给出一个未排序的整数数组,找出最长的连续元素序列的长度。

如:

给出[100, 4, 200, 1, 3, 2],

最长的连续元素序列是[1, 2, 3, 4]。返回它的长度:4。

你的算法必须有O(n)的时间复杂度 。

思路

  1. "排序转换成经典的动态规划问题"的话排序至少需要时间复杂度为O(nlog(n))——pass
  2. 利用c++中的set,直接会排序,并且没有重合的,但是set背后实现的原理牵扯到红黑树,时间复杂度不满足——pass
  3. 建立hash索引,把查找的元素周围的都访问个遍,求出个临时最大值跟全局最大值比较。当再次访问该段的元素的时候,直接跳过。这样保证时间复杂度为O(n),c++11中数据结构为unordered_set,保证查找元素的时间复杂度为O(1).

伪代码

建立无序集合existSet visitedSet分别表示原集合中包含的元素和已经访问了的元素
全局最大个数maxLen
顺序遍历原集合中的元素
临时计数count=1
如果该元素在visitedSet,停止往下执行,进行下一次循环
否则,把改元素小的并且在existSet中的元素存放在visitedSet中,count++
把改元素大的并且在existSet中的元素存放在visitedSet中, count++
maxLen = max(maxLen, count)
 class Solution {
public:
int longestConsecutive(vector<int> &num) {
int max=;
std::unordered_set<int> visit;
std::unordered_set<int> exist;
for(int i=;i<num.size();i++){
exist.insert(num[i]);
}
for(int i=;i<num.size();i++){
if(visit.find(num[i])!=visit.end()){
continue;
}
int count=;
int left=num[i]-;
while(exist.find(left)!=exist.end()){
count++;
visit.insert(left);
left--;
}
int right=num[i]+;
while(exist.find(right)!=exist.end()){
count++;
visit.insert(right);
right++;
}
if(count>max)
max=count;
}
return max;
}
};

最长连续序列(Longest Consecutive Sequence)的更多相关文章

  1. [Swift]LeetCode128. 最长连续序列 | Longest Consecutive Sequence

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

  2. leetcode 最长连续序列 longest consecutive sequence

    转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 2 ...

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

    2018-11-25 16:28:09 问题描述: 问题求解: 方法一.如果不要求是线性时间的话,其实可以很直观的先排序在遍历一遍就可以得到答案,但是这里明确要求是O(n)的时间复杂度,那么就给了一个 ...

  4. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

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

  7. [Swift]LeetCode298. 二叉树最长连续序列 $ Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

  9. LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

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

随机推荐

  1. 【java基础 17】集合中各实现类的性能分析

    大致的再回顾一下java集合框架的基本情况 一.各Set实现类的性能分析 1.1,HashSet用于添加.查询 HashSet和TreeSet是Set的两个典型实现,HashSet的性能总是比Tree ...

  2. 【转】hibernate映射(单向双向的一对多、多对一以及一对一、多对一)

    多对一关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是多指向一 一对多关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是一指向多 也就是说一对多和多对一的映射策略是一样的,只是站 ...

  3. 正则表达式与python中re模块

    一个网站,正则表达式入门的,很好 http://www.jb51.net/tools/zhengze.html 下面这个包含对python中re的介绍,也是很不错的http://www.w3cscho ...

  4. POJ——2236Wireless Network(暴力并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 22107   Accepted: 928 ...

  5. 刷题总结——拆网线(noip模拟 贪心)

    题目: 给定一颗树··在保证有k个点与其它点连接的情况下问最少保留多少条边···· 树的节点树n和k均小于100000: 题解: 很容易看出来我们要尽量保留那种一条边连两个节点的情况···· 然后考试 ...

  6. 刷题总结——生日礼物(bzoj1293单调队列)

    题目: Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠, ...

  7. PHP中的验证码类(验证码功能设计之二)

    运行结果: <!--vcode.class.php内容--> <?php class Vcode { private $width; //宽 private $height; //高 ...

  8. 2017-2018-2 20179204《网络攻防实践》linux基础

    我在实验楼中学习了Linux基础入门课程,这里做一个学习小结. 第一节 linux系统简介 本节主要介绍了linux是什么.发展历史.重要人物.linux与window的区别以及如何学习linux. ...

  9. AT&T汇编语言及其寻址方式

    汇编语言论风格来分主要是两类,一类是Intel汇编,一类是AT&T汇编,分别被Windows和Linux作为主流风格.因为我博客以推荐Linux系统为主,所以以后多以Linux汇编为主要描述语 ...

  10. php接口开发时,数据解析失败问题,字符转义,编码问题

    php接口开发时,数据解析失败问题,字符转义,编码问题 情景: A平台--->向接口请求数据---->接口向B平台请求数据---->B平台返回数据给接口---->接口返回数据给 ...