【题目】

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]的更多相关文章

  1. LeetCode——Longest Consecutive Sequence

    LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...

  2. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  3. LeetCode: Longest Consecutive Sequence 解题报告

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  4. [leetcode]Longest Consecutive Sequence @ Python

    原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of i ...

  5. [LeetCode] Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  6. Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. LeetCode—Longest Consecutive Sequence

    题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  8. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [Leetcode] Longest Consecutive Sequence 略详细 (Java)

    题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...

随机推荐

  1. aspx生成验证码

    //定义方法 public partial class VerificationCode : System.Web.UI.Page    {        string ImagePath = &qu ...

  2. PowerDesigner Mysql 主键自增、初始值、字符集

    自增 在你所要设为自增型的键上(比如你的id)双击,弹出一个Column Properties对话框,右下角有一个Identify的选择框,选中它OK,就可以了. 再去查看Preview,就能看到AU ...

  3. 0301——SearchController

    创建显示的页面 SearchViewController * searchVC = [[SearchViewController alloc]init]; 告诉搜索控制器将结果显示在创建的页面上 se ...

  4. hdu 2438

    Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...

  5. C#通过生成ini文件,记住用户关闭程序之前的选择+忽略跨线程检查

    1.在类的里面添加 //写配置文件 [DllImport("kernel32")] private static extern long WritePrivateProfileSt ...

  6. C#中弹出新窗口

    1.在主窗体程序中定义对应别的窗体的对象 Form_a_class form1 = Form_a_class  test_delegate(); 2.调用显示 form1.ShowDialog();

  7. JS常用的方法

    1.时间戳转换 //时间戳(有Date和无Date的都可)转换为日期 “2016年5月30日 10:29:30 2016-05-20 09:11” function TimeConversion(ti ...

  8. 【Nutch2.2.1基础教程之6】Nutch2.2.1抓取流程

    一.抓取流程概述 1.nutch抓取流程 当使用crawl命令进行抓取任务时,其基本流程步骤如下: (1)InjectorJob 开始第一个迭代 (2)GeneratorJob (3)FetcherJ ...

  9. JavaScript语法支持严格模式:"use strict"

    如果给JavaScript代码标志为“严格模式”,则其中运行的所有代码都必然是严格模式下的.其一:如果在语法检测时发现语法问题,则整个代码块失效,并导致一个语法异常.其二:如果在运行期出现了违反严格模 ...

  10. CMS设计-组件化

    原来CMS使用的专题类的页面是 : 事先由前端写好完整页面,再交付给运营使用,这样使用的比较灵活,可以根据市场的不同需求由前端切出不同的页面,有时候一旦需求过多,就耽误切图的时间. 现在M和H5采用组 ...