描述

输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 O(n) 。

输入

54,55,300,12,56

输出

3

通常我们看有没有连续序列时 找某个数有没有的前后的数,比如看到55,我们会下意识找56,57...54,53...这道题做法也是这样。

我的做法是用map映射,键为列表中的每个数,值为1。然后遍历数组,比如数为55,找这个数的前后有没有map映射后值为1的,有的话sum++,为了防止重复遍历需要把遍历过的数的映射值改成-1。

class Solution
{
public:
int longestConsecutive(vector<int>& nums)
{
map<int, int>M;
int len = nums.size(), ans = ;
for(int i = ; i < len; i++)
M[nums[i]] = ;
for(int i = ; i < len; i++)
{
if(M[nums[i]] == -) continue;
int sum = , j = nums[i];
while()
{
M[j] = -;
if(M[++j] == )
{
sum++;
continue;
}
break;
}
j = nums[i];
while()
{
M[j] = -;
if(M[--j] == )
{
sum ++;
continue;
}
break;
}
ans = max(ans, sum);
}
return ans;
}
};

【LeetCode-128】Longest Consecutive Sequence的更多相关文章

  1. 【LeetCode OJ】Longest Consecutive Sequence

    Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...

  2. 【leetcode】Longest Consecutive Sequence

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

  3. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  4. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  5. [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  6. 【leetcode】Longest Consecutive Sequence(hard)☆

    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 sequence. F ...

  8. LeetCode(128) Longest Consecutive Sequence

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

  9. 【LeetCode OJ】Longest Palindromic Substring

    题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

随机推荐

  1. C/C++结构体语法总结

    转自:http://blog.csdn.net/dawn_after_dark/article/details/73555562 结构体简介 结构体属于聚合数据类型的一类,它将不同的数据类型整合在一起 ...

  2. 【c++ primer, 5e】函数声明 & 分离式编译

    p186~p188: 函数声明1.函数只能定义一次,但是可以声明多次. 2.函数的接口:返回类型 + 函数名 + 形参类型 3.为什么要在头文件中进行函数声明???在源文件中定义?暂时理解到,这么做可 ...

  3. linux下 安装php的gettext模块

    安装php的模块有两种方式: 一.重新编译php,加上--with-gettext 二.动态安装 现在说下第二个动态安装 1.下载同版本的php原包,解压后进入ext目录,目录下便是模块 2.进入ge ...

  4. 在LAMP的生产环境内添加PHP的cURL扩展模块

    服务器运行一段时间后,可能突然会需求添加某个扩展,如curl.pdo.xmlrpc等, 这就需要在不重新编译 PHP   的情况下独立添加扩展. 下面以安装curl为例,介绍具体安装步骤. 1.安装c ...

  5. C#中将一个引用赋值null的作用

    有类A,以及A类型的变量a和b.初始化a之后,将a赋给b.之后将a赋为null.之后b还是可以使用. 思维误区:本来以为a=null之后,b也应该等于null. 实际测试效果如下 class Prog ...

  6. MySQL中表复制:create table like 与 create table as select

    1    CREATE TABLE A LIKE B此种方式在将表B复制到A时候会将表B完整的字段结构和索引复制到表A中来. 2.    CREATE TABLE A AS SELECT * FROM ...

  7. vue.js的一些小语法v-for,v-text,v-html,v-on:click

    1.Vue的目录结构: ======================================================================================== ...

  8. Keepalived安装配置入门

    准备两台虚拟机,IP如下: A:192.168.1.11 B:192.168.1.12 A为Master,B为BackUp 1.安装 yum install keepalived -y 2.配置 A服 ...

  9. java程序结构

    if....else.... 1.  if都需要接判断表达式 2.  else不需要表达式 3. 有if没else可以,但else必须要有一个if,if数>=else数 if (A条件)     ...

  10. 【分类】AlexNet论文总结

    目录 0. 论文链接 1. 概述 2. 对数据集的处理 3. 网络模型 3.1 ReLU Nonlinearity 3.2 Training on multiple GPUs 3.3 Local Re ...