描述

输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 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. AtCoder Regular Contest 080 E - Young Maids

    地址:http://arc080.contest.atcoder.jp/tasks/arc080_c 题目: E - Young Maids Time limit : 2sec / Memory li ...

  2. java对象在JVM堆中的数据结构

    java对象和数组是存放在堆中的,那么这些instance的数据结构是什么呢? 对象头:对象头存放的是这个对象的一些元数据信息.例如每个对象都有哈希值,GC分代年龄,锁状态标志等,这些信息就是存放在对 ...

  3. PHP分页及原理

    在看本文之前,请确保你已掌握了PHP的一些知识以及MYSQL的查询操作基础哦. 作为一个Web程序,经常要和不计其数的数据打交道,比如会员的数据,文章数据,假如只有几十个会员那很好办,在一页显示就可以 ...

  4. Swoole学习(四)Swoole之简单WEB服务器的创建

    环境:Centos6.4,PHP环境:PHP7 <?php //创建WEB服务器 $host = '0.0.0.0'; $port = ; $server = new swoole_http_s ...

  5. Python3.x:chrome运行webdriver脚本提示--ignore-certificate-errors

    Python3.x:chrome运行webdriver脚本提示--ignore-certificate-errors 1,分析原因: 根本原因是Chromedriver和Chrome的版本不兼容: 网 ...

  6. Zabbix 关联onealert实现电话报警

    Zabbix 关联onealert实现电话报警 系统环境:Linux Centos 7.4 应用版本:Zabbix 3.0.22 OneAlert官方地址:http://www.onealert.co ...

  7. 20145329 《Java程序设计》第三周学习总结

    教材学习内容总结 java并非完整的面向对象程序语言 定义 1:class:定义类 2.char:类型声明变量 3.new:新建对象 4.名称 X:参考 5.=:可用于指定参考至新建变量 6.构造函数 ...

  8. Java Mail 邮件发送Demo

    上周公司的项目要求开发邮件发送功能.自己在网上跟着教程边学边做了一下午,现在基本开发完成了.由于一个同事也想看下该怎么写,顺便学习下.所以我就写成了一遍教程,顺便巩固下邮件发送里面的内容. Demo ...

  9. 动态 K th

    每一棵线段树是维护每一个序列前缀的值在任意区间的个数,如果还是按照静态的来做的话,那么每一次修改都要遍历O(n)棵树,时间就是O(2*M*nlogn)->TLE考虑到前缀和,我们通过树状数组来优 ...

  10. maven问题:如何启动maven项目

    maven是项目构建工具,用于解决jar间的依赖,启动maven项目的命令:tomcat:run 步骤如下: 1.在pom.xml文件中配置插件,此处配置的是tomcat8 2.右击项目名,找到Run ...