题目描述:

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 无外网情况下安装 mysql

    由于工作需要,需要在一台装有 CentOS 系统的测试服务器上安装 MySQL ,由于该服务器上存有其他比较重要的测试数据,所以不能连接外网.由于之前安装 MySQL 一直都是使用 yum 命令一键搞 ...

  2. phoenix系统创建语句

    CREATE TABLE SYSTEM."CATALOG"( TENANT_ID VARCHAR NULL, TABLE_SCHEM VARCHAR NULL, TABLE_NAM ...

  3. c#省市联动(sqlHelper的应用)

    sqlHelper: using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...

  4. openvpn 移植之buildroot添加相关选项

    openvpn 移植第一步,在buildroot 内添加 openssl ,openvpn , 另外还有一个 RSA 的支持,我不确定这个需要程度如何,但是也添加进去了. buildroot 添加相关 ...

  5. 使用padding和float处理带有间隙的多块布局

    . 每个间隙都是20px <div class="action-content pd10" style=""> <div class=&quo ...

  6. java线程池的应用浅析

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java ...

  7. Tomcat性能优化(二) 启动参数设置

    一.tomcat绿色版设置方法 进入tomcat/bin目录下,找到catalina.bat文件在文件首行中插入下面这段配置即可. set JAVA_OPTS=-server -Djava.awt.h ...

  8. awk 计算数据的和和平均值

    awk 计算数据的和和平均值 2014年12月02日 21:11:12 HaveFunInLinux 阅读数:14487更多 个人分类: 小技巧   本文译至:http://d.hatena.ne.j ...

  9. 打开palette控制面板

    (2)

  10. linux 测试工具

    最近在寻找linux的测试工具,试用了一些.记录如下. memtester 内存测试工具,通过对内存进行读写进行测试.可以对同一块空间进行多次的读写. 源码分析 http://www.cnblogs. ...