题目描述:

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(NlogN),不符合要求。

以空间换时间:

代码:

int longestConsecutive(vector<int>& nums)
{
map<int,int> map1;
int len = 0;
int maxlen = len;
for(int i = 0;i<nums.size();i++)
{
//在nums中出现,则不为0,否则为0
map1[nums[i]]++;
}
int min1 = nums[0];
int min_up = min1;
int min_down = min1-1;
int i = 0;
while(i<nums.size())
{
if(map1[min_up]!=0)
{
len++;
map1[min_up] = 0;
min_up++;
}
if(map1[min_down]!=0)
{
len++;
map1[min_down] = 0;
min_down--;
}
if(map1[min_down]==0 && map1[min_up]==0)
{
if(maxlen<len)
maxlen = len;
cout<<maxlen<<endl;
len = 0;
while(map1[nums[i++]]==0&&i<nums.size());
if(i<nums.size())
{
min1 = nums[i-1];
min_up = min1;
min_down = min1-1;
}
else break;
}
}
return maxlen;
}

时间复杂度:O(N),同时引入了map,空间复杂度O(N).

LeetCode—Longest Consecutive Sequence的更多相关文章

  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 [128]

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

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

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

  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. [linux]signal函数不起作用

    #include "apue.h" #include <sys/wait.h> static void sig_int(int); /* our signal-catc ...

  2. dp之完全背包poj1787(完全背包以及路径记录 推荐)

    题意:有四种硬币,1分,5分,10分,25分,分别有a,b,c,d种,给出一个n分钱,要求你求出组成n分钱最多需要的硬币数量,并且输出组成它的各种硬币的数量...... 学到的东西:这个题目我是用两种 ...

  3. 纯css3实现的win8加载动画

    今天给大家分享一款纯css3实现的win8加载动画.在这款实例中动画效果完全由css3实现.一起看下效果图: 在线预览   源码下载 实现的代码. html代码: <div class=&quo ...

  4. mof提权原理及其过程

    mof提权的原理: mof是windows系统的一个文件(在c:/windows/system32/wbem/mof/nullevt.mof)叫做"托管对象格式"其作用是每隔五秒就 ...

  5. spark读取本地文件

    /** * Read a text file from HDFS, a local file system (available on all nodes), or any * Hadoop-supp ...

  6. Unix系统编程()发送信号的其他方式:raise和killpg

    有时,进程需要向自身发送信号,raise 函数就执行了这一任务. #include <signal.h> int raise(int sig); 在单线程程序中,调用raise相当于对ki ...

  7. C语言 · 素数判断

     算法提高 素数判断   时间限制:1.0s   内存限制:512.0MB      编写一函数IsPrime,判断某个大于2的正整数是否为素数. 样例输入: 5样例输出:yes 样例输入: 9样例输 ...

  8. su和sudo命令

    su命令用于在不同的用户之间切换,比如使用user1登陆了系统,但要执行一些管理操作,比如useradd,普通用户没有这个权限的,解决的办法有两个. 1:退出user1用户,重新以root用户登录系统 ...

  9. 如何对MySQL中的大表进行数据归档

    使用MySQL的过程,经常会遇到一个问题,比如说某张”log”表,用于保存某种记录,随着时间的不断的累积数据,但是只有最新的一段时间的数据是有用的:这个时候会遇到性能和容量的瓶颈,需要将表中的历史数据 ...

  10. 回车替换Tab 并不会 提交表单 IE Chrome 通过

    网上一堆可以回车替换tab的代码,可是基本都忽略谷歌浏览器的兼容性,找了3个小时 试了无数遍,终于总结出这一段代码,希望能帮到需要的同学,也给自己留个备忘        document.onkeyd ...