最长连续序列(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 ...
随机推荐
- requests与urllib 库
requests库 发送请求: 可以处理所有请求类型:get.post.put.Delete.Head.Options r = requests.get(''https://httpbin.org/' ...
- Navicat数据库备份还原
Navicat数据库备份包括数据和架构 还原数据库:把备份文件复制到备份文件夹所在位置,还原即可
- 读《MySql必知必会》笔记
MySql必知必会 2017-12-21 意义:记录个人不注意的,或不明确的,或不知道的细节方法技巧,此书250页 登陆: mysql -u root-p -h myserver -P 9999 SH ...
- hdu6085[压位+暴力] 2017多校5
/*hdu6085[压位+暴力] 2017多校5*/ /*强行优化..*/ #include <bits/stdc++.h> using namespace std; struct bit ...
- Centos7系统rc.local不起作用问题
Centos7系统rc.local不起作用问题 来源 https://www.cnblogs.com/xjz00/p/7729405.html Centos7已经写了要chmod +x /etc/rc ...
- hihoCoder #1157 建造金字塔
这道题我想了一天才想清楚做法.AC 了之后去看别人写的题解,都是三言两语意识流式描述,我并不能读懂.我觉得很自卑,为何人家解这道题如此轻松.不过,我还是决定把我的解法写下来,并且一定要写清楚. 思路 ...
- Server-Side Rendering(服务端渲染)的优点与缺点
优点 1. SEO 客户端渲染,页面中只有初始的几个html容器,js生成内容填充到容器中,爬虫只能识别到初始的html容器,js生成的内容一般不会被识别,而服务端渲染直接给出html,爬虫可以识别到 ...
- Qtree
Qtree Ⅰ 题意:https://vjudge.net/problem/SPOJ-QTREE 带修路径查询最大边权 sol :树链剖分,之后每条重链就是一个连续的区间,拿线段树维护即可 简单讲讲 ...
- [暑假集训--数位dp]hdu5787 K-wolf Number
Alice thinks an integer x is a K-wolf number, if every K adjacent digits in decimal representation o ...
- 实时监控linux
https://www.cnblogs.com/tinywan/p/6826125.html