最长连续序列(Longest Consecutive Sequence)
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)的时间复杂度 。
思路
- "排序转换成经典的动态规划问题"的话排序至少需要时间复杂度为O(nlog(n))——pass
- 利用c++中的set,直接会排序,并且没有重合的,但是set背后实现的原理牵扯到红黑树,时间复杂度不满足——pass
- 建立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)的更多相关文章
- [Swift]LeetCode128. 最长连续序列 | Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- leetcode 最长连续序列 longest consecutive sequence
转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 2 ...
- 最长连续子序列 Longest Consecutive Sequence
2018-11-25 16:28:09 问题描述: 问题求解: 方法一.如果不要求是线性时间的话,其实可以很直观的先排序在遍历一遍就可以得到答案,但是这里明确要求是O(n)的时间复杂度,那么就给了一个 ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [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 ...
- [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 ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
随机推荐
- Linux免密远程登陆
上一节讲到伪分布式部署,启动后需要输入4次密码,停止服务后也要输入4次密码.本节记录免密登陆原理和实践 假设有2台服务器(A和B)(这是配置原理) 1)A需要远程登录B服务器,那么A就要创建密钥对(私 ...
- linux下java命令行引用jar包
一般情况下: 如果java 文件和jar 包在同一目录 poi-3.0-alpha3-20061212.jar testTwo.java 编译: javac -cp poi-3.0-alpha3-2 ...
- 谈谈Python中对象拷贝
你想复制一个对象?因为在Python中,无论你把对象做为参数传递,做为函数返回值,都是引用传递的. 何谓引用传递,我们来看一个C++交换两个数的函数: void swap(int &a, in ...
- hihoCoder #1246 王胖浩与环
题目大意 $n$($1\le n\le 2000$)个正整数 $a_1, a_2, \dots, a_n$($a_i\le 5\times 10^7$)分布在一个圆环上. 定义 $b_k$ 为:将环上 ...
- 洛谷 P3391 模板Splay
#include<bits/stdc++.h> using namespace std; #define maxn 200000 int read() { ,w=; ;ch=getchar ...
- python解析yaml文件
YAML语法规则: http://www.ibm.com/developerworks/cn/xml/x-cn-yamlintro/ 下载PyYAML: http://www.yaml.org/ 解压 ...
- zoj 2974 Just Pour the Water矩阵快速幂
Just Pour the Water Time Limit: 2 Seconds Memory Limit: 65536 KB Shirly is a very clever girl. ...
- 两个时间相差多少 .net中的timespan应用
原文发布时间为:2008-10-31 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- 应用gulp工具构建个自动算rem布局的小例子
因为最近可能需要做移动端rem布局,因为rem布局需要将px转化成rem,如果次都需要拿计算器算就太low了,所以就想到用less和gulp. 因为也是初学gulp,站点的文件结构还没想到太好,也只是 ...
- Javascript 开启浏览器全屏模式
作者:伯乐在线/前端空城师 通常在某些情况下,我们需要让浏览器开启全屏模式,以便获得更好的视觉体验,先看下全屏模式简单的几个API. 浏览器默认绑定 非全屏模式下, document的F11按键绑定开 ...