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/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- spring-data-mongodb必须了解的操作
http://docs.spring.io/spring-data/data-mongo/docs/1.0.0.M5/api/org/springframework/data/mongodb/core ...
- LoadRuner性能测试之内存分析方法及步骤(Windows)
1.首先观察Available Mbytes(可用内存),至少要>=1/2的内存空间 2.然后观察Pages/sec值是不是很大 3.再观察Page Faules/sec是不是很大,其值表示 ...
- Samba通过ad域进行认证并限制空间大小《转载》
本文实现了samba服务被访问的时候通过windows域服务器进行用户名和密码验证;认证通过的用户可以自动分配500M的共享空间;在用户通过windows域登陆系统的时候可以自动把这块空间映射成一块硬 ...
- FTS下载地址
http://download.microsoft.com/download/5/2/e/52e22b90-2ba7-427b-9ea4-604d3b37a2e7/vs2012_tfs_chs.iso
- Eclipse搭建Android开发环境(安装ADT,Android4.4.2)(转)
使用Eclipse做Android开发,需要先在Eclipse上安装ADT(Android Development Tools)插件. 1.安装JDK 1.7 JDK官网http://www.orac ...
- oracle插入例子
string sql = "insert into EMST_JC_SBXX(XL,SBBM,SBWH,SBMC,CCBM,XNCS,CZXL,ZL,GL,ZZCJ,TCRQ,SYQX,XH ...
- 由MyEclipse内存不足谈谈JVM内存设置
转自:http://www.javatang.com/archives/2007/12/03/1653250.html 如果没有进行设置的话,在使用MyEclipse的经常出现如下图所示内存不足的提示 ...
- exc_bad_access(code=1, address=0x789870)野指针错误
原因: exc_bad_access(code=1, address=0x789870)野指针错误,主要的原因是,当某个对象被完全释放,也就是retainCount,引用计数为0后.再去通过该对象去调 ...
- win7添加usb3.0驱动(错误代码1392,文件或目录损坏且无法读取)
Win7添加usb3.0驱动 之前一直按照网上的方法执行dism命令挂载时,总是失败,错误代码1392,显示原因是文件或目录损坏且无法读取.这个错误以前在装机时老是出现导致系统安装不成功,在BIOS中 ...
- c#快捷键设置和text输入限制
快捷键 使用KeyDonw事件 输入限制使用 KeyPress 事件 1.注意:如果是整个窗体的快捷键,一定要把窗体属性中的KeyPreview改为true private void textbox_ ...