【题目】

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. uva 111 History Grading(最长公共子序列)

    题目连接:111 - History Grading 题目大意:给出一个n 代表序列中元素的个数, 然后是一个答案, 接下来是若干个同学的答案(直到文件结束为止), 求出两个序列的最长公共子序列, 注 ...

  2. 监控工具cacti

    一. 安装 cacti服务端 1. 首先要安装epel扩展源yum install -y epel-release2. (lamp)然后分别安装httpd.php.mysqlyum install - ...

  3. c#、sql数据库备份还原

    1.在项目中添加SQLDmo dll文件引用(SQLDMO(SQL Distributed Management Objects,SQL分布式管理对象)) 2在相应页面加using SQLDMO引用 ...

  4. C# 对象深复制

    Mark: //实现IClonable接口并重写Clone方法就可以实现深克隆了 #region ICloneable 成员 public object Clone() { MemoryStream ...

  5. sharding的基本思想和理论上的切分策略

    本文着重介绍sharding的基本思想和理论上的切分策略,关于更加细致的实施策略和参考事例请参考我的另一篇博文:数据库分库分表(sharding)系列(一) 拆分实施策略和示例演示 一.基本思想 Sh ...

  6. java 面对对象(抽象 继承 接口 多态)

    什么是继承? 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可. 多个类可以称为子类,单独这个类称为父类.超类或者基类. 子类可以直接 ...

  7. Linux下使用多线程模拟异步网络通信

    服务器 /* socket server * 2014-12-15 CopyRight (c) arbboter */ #include <unistd.h> #include <s ...

  8. 三个重要的游标sp_cursoropen

    請問這三個存諸過程的作用是什么﹖ sp_cursoropen, sp_cursorfetch, sp_cursorclose API 服务器游标实现  SQL Server OLE DB 提供程序. ...

  9. Java系列--第二篇 基于Maven的Android开发HelloAndroidWorld

    曾经写过一篇Android环境配置的随笔,个人感觉特繁琐,既然有Maven,何不尝试用用Maven呢,经网上搜索这篇文章但不限于这些,而做了一个基于Maven的Android版的Hello Andro ...

  10. map和reduce

    map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. map()传入的第一个参数是f,即函数对象本身.由于 ...