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. Linux里如何设置IP(RED HAT)

    一共包括以下两步 1:通过setup命令设置IP 保存…… --如果‘Use DHCP'处事[*],则可能无法手动输入IP,子网掩码和网关. 解决办法: 修改脚本/etc/sysconfig/netw ...

  2. commons.fileupload简单应用

    导入包: commons-fileupload-1.3.1.jar commons-io-2.4.jar commons-fileupload依赖于commons-io,commons-io-2.4必 ...

  3. keil c51中C程序的启动过程

    汇编是从org 0000h开始启动,那么keil c51是如何启动main()函数的?keil c51有一个启动程序startup.a51,它总是和c程序一起编译和链接.下面看看它和main()函数是 ...

  4. tp28xx port pin (open-drain )and (push-pull) 和open collector)

    具有开漏(OD)输出的器件是指内部输出和地之间有个N沟道的MOSFET(T1),这些器件可以用于电平转换的应用.输出电压由Vcc'决定.Vcc'可以大于输入高电平电压VCC(up-translate) ...

  5. SQL Server聚合函数

    聚合函数对一组值计算后返回单个值.除了count(统计项数)函数以外,其他的聚合函数在计算式都会忽略空值(null).所有的聚合函数均为确定性函数.即任何时候使用一组相同的输入值调用聚合函数执行后的返 ...

  6. Linux上的设备管理器

    一般windows上我们用它自带的“设备管理器”来查看,管理,安装,卸载驱动. 那么问题来了,Linux上用什么命令来看呢? 可以用: lshw   lsusb lspci lsmod 查看特定模块. ...

  7. C/C++四种退出线程的方法

    退出线程可以有四种方法: 1.线程函数的return返回(最好这样): 其中用线程函数的return返回, 而终止线程是最安全的, 在线程函数return返回后, 会清理函数内申请的类对象, 即调用这 ...

  8. UVa232.Crossword Answers

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. 游标的使用实例(Sqlserver版本)

    游标,如果是之前给我说这个概念,我的脑子有二个想法:1.你牛:2.我不会 不会不是理由,更不是借口,于是便要学习,本人属性喜欢看代码,不喜欢看书的人,所以嘛,文字对我没有吸引力:闲话少说啊,给大家提供 ...

  10. Thinkphp交友手机首页简明前台、后台

    先来说下后台吧,后台要写后台模板-V,后台控制器-C 后台模板如下代码: <!DOCTYPE html> <html> <head> <meta charse ...