描述

输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 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. split_lzo_lib.sh

    split_lzo_lib.sh #!/bin/sh#输入文件名filename=$1#分割文件大小filesize=4096#输出库文件名libname="lib"$(echo ...

  2. web前端基础——初识JavaScript

    1 JavaScript概述 JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚 ...

  3. C++之条形码,windows下zint库的编译及应用(二)

    zint库是一个开源的第三方库,提供了生成条形码.二维码等功能.本文主要介绍zint库的生成及简单应用.   0windows下zint库的编译及应用(一)   工具/原料   vs2012 生成条形 ...

  4. handle 和module

    <httpHandlers> <add verb="*" path="*" type="ClassLibrary831.TestHa ...

  5. 机器学习与R语言:kNN

    #---------------------------------------- # 功能描述:演示kNN建模过程 # 数据集:威斯康星乳腺癌诊断 # #---------------------- ...

  6. Fiddler4调试工具配置使用笔记

    Fiddler最大的用处: 模拟请求.修改请求.手机应用调试 Fiddler最新版本 下载地址: http://www.telerik.com/download/fiddler Fiddler 想要监 ...

  7. HDFS并行复制Distcp

    1)Distcp(分布式拷贝)是用于大规模集群内部和集群之间拷贝的工具. 2)distcp命令是以MR作业(没有R任务)的形式实现的,把文件和目录的列表作为M任务的输入.每一个文件是由一个M任务来拷贝 ...

  8. arp攻击的处理方法

    http://www.hacking-tutorial.com/tips-and-trick/4-steps-to-prevent-man-in-the-middle-attack-arp-poiso ...

  9. MySQL 常用运算符

    1.算数运算符 加 mysql> select 1+2; 减 mysql> select 2-1; 乘 mysql> select 2*3; 除 mysql> select 5 ...

  10. 爬虫框架Scrapy之Spider

    Spider Spider类定义了如何爬取某个(或某些)网站.包括了爬取的动作(例如:是否跟进链接)以及如何从网页的内容中提取结构化数据(爬取item). 换句话说,Spider就是您定义爬取的动作及 ...