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. VirtualBox虚拟机无法选择桥接方式

    VirtualBox 装好之后默认的网络是NAT模式,但这种模式中虚拟机配置的IP和主机的不再同一网段内,无法获得和主机一样的局域网地位,更不可能从主机用远程桌面携带硬盘的方式远程控制.而最简便的方法 ...

  2. unix c 02

    环境变量 - 存储在内存中的信息,格式是映射,作用就是 帮助系统 进行一些工作,一般是 查找某个东西. 预处理指令:#warning #error #pragma 使用程序直接调用库文件的函数(动态编 ...

  3. SQL查询最近三个月的数据(查询最近几天,几年等等)

    定义和用法      :: 天,这样就可以找到付款日期. 我们使用如下 ,OrderDate) AS OrderPayDate FROM Orders 结果: OrderId    OrderPayD ...

  4. CString 的一些事

    MFC Visual Studio 2008 CString 的 Format 中不能这样存在str.Format(_T("Cool(\%)"));  或者 str.Format( ...

  5. extjs两个tbar问题

      版本:extjs3.4   接触过extjs的同志们都知道每个panel都有一个tbar(top bar 上面工具栏) ,bbar(bottom bar 底部工具栏)      大家做查询页面,一 ...

  6. YYmodel 郭耀源 底层分析

    http://www.tuicool.com/articles/meAzIny         YYModel 简介与使用 http://www.jianshu.com/p/663c7b608ff5 ...

  7. Mysql日期函数,时间函数使用的总结

    一.MySQL 获得当前日期时间 函数 1.1 获得当前日期+时间(date + time)函数:now() mysql> select now();+--------------------- ...

  8. [Regular Expressions] Match the Start and End of a Line

    We can use: ^: match the beginning $: match the end Let's say we have the string like the following: ...

  9. Linux 挂载命令 --mount

    1.挂载光盘命令  mount :  mount [-t vfstype] [-o options] device dir mount [-t 文件系统] [-o 特殊选项] 设备文件名 挂载点 -t ...

  10. ASP.NET母版与内容页相对路径的问题

    1. 图片问题 非常好解决 <img runat="server" src="~/images/ad468x60.gif" alt="" ...