LeetCode_Longest Consecutive Sequence
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的更多相关文章
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LintCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 24. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
随机推荐
- XJOI网上同步训练DAY3 T1
思路:看来我真是思博了,这么简单的题目居然没想到,而且我对复杂度的判定也有点问题.. 首先我们选了一个位置i的b,那一定只对i和以后的位置造成改变,因此我们可以这样看: 我们从前往后选,发现一个位置的 ...
- PowerShell为什么强大
PowerShell为什么强大 本文索引 [隐藏] 5.1举例介绍 1破天荒的方便 2面向对象 3绑上.NET这棵大树 4强大的兼容性 5基于平台的可扩展性 微软是一个很”低调”的公司,取名为微软,感 ...
- 2015第14周五Tomcat版本
首先看tomcat官方文档,列出的不同版本的主要差别: Servlet Spec JSP Spec EL Spec WebSocket Spec Apache Tomcat version Actua ...
- visual studio 中GIT的用法
http://msdn.microsoft.com/zh-cn/library/vstudio/hh850445 Git 使用最新版:Git-1.8.4-preview20130916http: ...
- uva1220--树的最大独立集+判重
题意是挑选尽量多的人,并且每个人都不和他的父节点同时出现,很明显的最大独立集问题,难点在于如何判断方案是否唯一. 详情请见刘汝佳<算法竞赛入门经典--第二版>P282 #include&l ...
- ubuntu下php安装xdebug
1.安装 sudo apt-get install php5-xdebug 2.配置 修改 php .ini 路径: /etc/php5/apache2/php.ini (这里可能有不同,不同的u ...
- PHP连接Mysql服务器的操作
我们的数据存储在数据库中以后,要把数据和网页联系起来的话,要通过web服务器的解释器进行读取数据,再传递给客户端网页.如图: 这里,我选择了PHP作为学习的解释器.下面就具体来总结一下PHP连接MYS ...
- 跨服务器查询sql语句样例
若2个数据库在同一台机器上:insert into DataBase_A..Table1(col1,col2,col3----)select col11,col22,col33-- from Data ...
- 利用Unicorn和Idaemu辅助解决Geekpwn SecretCode
在前面的些文章里,我提到了怎么交叉编译Unicorn-engine,以及在windows上使用Unicorn python bindings进行分析程序.这一次我介绍下如何使用Unicorn-engi ...
- date命令小结
date命令是查看日期时间的常用命令,date MMDDhhmmYY.ss(修改顺序)用来更改时间 linux时间分为系统时间和硬件时间, [root@www doc]# clock--------- ...