LeetCode: Longest Consecutive Sequence [128]
【题目】
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)
【思路】
O(n)不一定是one pass, 脑子里总是有这么种固定思维,自己给自己挖了个坑。O(kn)也是O(n), 仅仅要k是常数。
要找连续串,又不能排序,那我们要构造一个类列表结构把连续的数串起来。
那么怎么串呢?非常显然,给定一个数N。那我们须要知道他的前一个数prev和它的后一个数next是否存在,假设存在我们就能够串到prev或者next, 假设不存在则连续串就结束鸟。我们用一个map来表示这样的前后继关系。
prev=N-1; next=N+1; 假定N是存在于数组中的。则map[N]=1
假设prev也在数组中,则map[prev]=1, 否则map[prev]=0
假设next也在数组中,则map[next]=1, 否则map[next]=0
我们对数组进行两遍扫描:
第一遍扫描是我们生成前后关系map
第二遍扫描利用前后关系恢复连续串,恢复过程中相应数的map[i]都置为0,避免反复恢复
【代码】
class Solution {
public:
int longestConsecutive(vector<int> &num) {
int size=num.size();
if(size==0)return 0;
//第一遍扫描建立前后关系map
map<int, int> exist;
for(int i=0; i<size; i++){
exist[num[i]]=1;
if(exist[num[i]-1]!=1)exist[num[i]-1]=0;
if(exist[num[i]+1]!=1)exist[num[i]+1]=0;
}
//第二遍扫描
int maxLength=0;
for(int i=0; i<size; i++){
if(exist[num[i]]==1){
//恢复串
int length=1;
int number=num[i]-1;
while(exist[number]==1){length++; exist[number]=0; number--;}
number=num[i]+1;
while(exist[number]==1){length++; exist[number]=0; number++;}
if(length>maxLength)maxLength=length;
}
}
return maxLength;
}
};
LeetCode: Longest Consecutive Sequence [128]的更多相关文章
- LeetCode——Longest Consecutive Sequence
LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode: Longest Consecutive Sequence 解题报告
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [leetcode]Longest Consecutive Sequence @ Python
原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of i ...
- [LeetCode] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode—Longest Consecutive Sequence
题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [Leetcode] Longest Consecutive Sequence 略详细 (Java)
题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- 破解Veeam过程
1)运行Veeam_Backup_Setup.exe,但是不要继续下一步: 2)进入到%temp%\IXP000.TMP目录,例如windows xp sp3环境默认为C:\Documents and ...
- MySQL复制协议
http://hamilton.duapp.com/detail?articleId=27
- AngularJs练习Demo1
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- Oracle System密码忘记 密码修改、删除账号锁定lock
一下转自http://www.cnblogs.com/yjhrem/articles/2340149.html 运行cmd命令行 录入 sqlplus /nolog 无用户名登录 conn /as ...
- CSS书写规范、顺序和命名规则
写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,这里总结一个CSS书写规范.CSS书写顺序供大家参考 这些是参考了国外一些文章以及我的个 ...
- 移动web前端的一些硬技能(一)关于移动设备
移动端web其实也就是小尺寸触摸屏设备的web,其中包含了两个词——1.小尺寸设备:2.触摸屏.能把这两个问题给解决掉,就已经是一个基本合格的移动web前端开发工程师了.对于前端工程师来说,前路漫漫, ...
- css3实现各种渐变效果,比较适合做手机触屏版
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 自定义ListView分割线
要完成自定义分割线,我们先来认识一下listview中的两个属性: android:divider 设置list 列表项的分隔条(可用颜色分隔,也可用Drawable分隔) android:divid ...
- IOS 项目问题总结
把自己项目中遇到的问题总结一下,供大家参考,希望大家多多提出意见!! 在Xcode 6.2中遇到Your build settings specify a provisioning profile w ...
- block(三)揭开神秘面纱(上)
block到底是什么 我们使用clang的rewrite-objc命令来获取转码后的代码. 1.block的底层实现 我们来看看最简单的一个block: [caption id="attac ...