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/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- hdu 4666 Hyperspace
曼哈顿距离,两个点设为(x1,y1),(x2,y2),其距离为|x1-x2|+|y1-y2| #include <cstdio> #include <set> #include ...
- Windows下命令行直接编译程序
D:\> cl hello.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86 Cop ...
- [Javascript] Task queue & Event loop.
Javascript with Chorme v8 engine works like this : For Chorme engine, v8, it has call stack. And all ...
- OpenWrt sscanf问题之于MT7620N与AR9341
在MT7620N平台做好了wifidog的相关调试工作,除了eth驱动.wireless性能问题,其余的都能够基本正常. 依据实际须要要对已完毕的工作在AR9341平台上实现. 事实上也简单.基本功能 ...
- 利用JS实现简单的瀑布流效果
哈哈, 我又来啦, 在这一段时间里, 我简单的学习了一下javascript(JS), 虽然不是很懂啦, 但是我也简单的尝试着做了点小东西, 就比如现在流行的瀑布流效果, 经过我的努力终于成功的完成了 ...
- NSString属性什么时候用copy,什么时候用strong?【转】
转自:http://www.cocoachina.com/ios/20150512/11805.html. 我们在声明一个NSString属性时,对于其内存相关特性,通常有两种选择(基于ARC环境): ...
- 应用highcharts做直观数据统计
最近在看上了统计类的东东,发现以前端图表神器:highcharts Highcharts是一款功能强大.开源.美观.图表丰富.兼容绝大多数浏览器的纯Js图表库,Highcharts支持的图表类型有直线 ...
- asp.net 参数形式写sql
OracleConnection conn = c.GetConnection(); OracleCommand cmd = new OracleCommand(); ...
- Android原生Calendar代码阅读(一)
原生Calendar代码: 5.0Calendar源码.rar 提取的JavaDoc: Calendar的javadoc.rar 1. AsyncQueryService和AsyncQueryServ ...
- smarty 常用参数
section的产生是为解决foreach的不足的,与foreach一样,它用于设计模板内的循环块,它较为复杂,可极大程序上满足程序需要,所以在程序中我习惯使用它而不使用foreach,基本原形为:{ ...