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.

  The key point is that for each number say Val in the array, we only need to determine whether its neighbor (Val-1 and Val+1) is in the array and  for a consecutive sequence we only need to know the the boundary and the length information。

class Solution {
public:
int longestConsecutive(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int size = num.size();
if(size < ) return size; map<int, int> myMap;
for(int i = ; i< num.size(); i++)
myMap.insert(pair<int, int>(num[i],)); int maxLen = ;
map<int, int>::iterator m_it;
map<int, int>::iterator temp_it; for(m_it = myMap.begin(); m_it != myMap.end();m_it++)
{ if(m_it->second == ) continue;
int len = ;
int low = m_it->first;
int high = low;
while(true)
{
temp_it= myMap.find(low-);
if(temp_it == myMap.end())
break;
low--;
len++;
temp_it->second = ;
} while(true)
{
temp_it= myMap.find(high+) ;
if(temp_it == myMap.end())
break;
high++;
len++ ;
temp_it->second = ;
} maxLen = maxLen < len ? len : maxLen ; } return maxLen ; }
};

LeetCode_Longest Consecutive Sequence的更多相关文章

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

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

  2. [LeetCode] Longest Consecutive Sequence 求最长连续序列

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

  3. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

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

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

  5. 128. Longest Consecutive Sequence(leetcode)

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

  6. [LintCode] Longest Consecutive Sequence 求最长连续序列

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

  7. LeetCode Binary Tree Longest Consecutive Sequence

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

  8. 24. Longest Consecutive Sequence

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

  9. 【leetcode】Longest Consecutive Sequence

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

随机推荐

  1. Shell函数返回值、删除函数、在终端调用函数

    Shell 也支持函数.Shell 函数必须先定义后使用. Shell 函数的定义格式如下: function_name () { list of commands [ return value ] ...

  2. UVA10304---(区间DP)

    第一开始想着枚举根节点,然后记忆化搜索..结果TLE,最后还是看了一眼题解瞬间明白了..唉,还是思维太局限了 由于数据是按照从小到大排列的,可以自然地组成一颗二叉排序树. 设dp[i][j]是区间[i ...

  3. ora-28056 (Writing audit records to Windows Event Log failed)

    系统:windows xp oracle 版本 SQL> select * from v$version; BANNER ------------------------------------ ...

  4. JUnit三分钟教程 ---- 实际应用

    JUnit三分钟教程 ---- 实际应用 摘自http://lavasoft.blog.51cto.com/62575/65775   接上文"JUnit三分钟教程 ---- 快速起步&qu ...

  5. Linux配置FTP服务器

    基于CentOS-6.5 1.先查看是否已安装:rpm -qa vsftpd 或 rpm -ql vsftpd 2.安装:yum install vsftpd -y 3.设置开机启动: chkconf ...

  6. 【转】Windows与Linux(Ubuntu)双系统时间不一致的解决方法

    当在嵌入式Linux里面备份文件时候,在备份的时候,PC(win7)和开发板的时间都是9:30,但是在开发板发现文件创建时间是9:30,然后u盘插在PC(win7)上,发现文件创建时间是1:30,为什 ...

  7. redis安装配置和使用;tomcat安装和使用

    virtualbox主要有以下几种方式(不同版本号称法不一样,但实质是一样的): 1.Intelnal Network:利用主机上的全部的虚拟机构建一个虚拟网络 2.NAT:能訪问互联网,不能訪问主机 ...

  8. 要理解javascript中间apply和call

    apply和call它是javascript一个非常重要的方法,.虽然与程序平时很少接触,但JS到处都在使用这个框架2方法. 2个方法是在Function.prototype中.也就是说每一个JS函数 ...

  9. 制作可独立分发的Android模拟器

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6586759 如果我们编写了一个Android应 ...

  10. js中json的转换

    //aa='{"id":0,"appId":"app***********Id","appSecret":"a ...