128. 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.
===================
给一个未排序数组,找到一个最长的连续数组,返回其长度.
====
思路:
利用一个hash表,存储已经扫描的数组元素,
对数组中每一个curr元素,都必须在hash表中向前查找,向后查找,找出此nums[curr]过在连续数组。
maxlength = max(maxlength,length);
===
code:
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_map<int,bool> hash_used;
for(auto i:nums){
hash_used[i] = false;
}
int longest = ;
for(auto i:nums){
if(hash_used[i])
continue;
int length = ; hash_used[i] = true; for(int j = i+;hash_used.find(j)!=hash_used.end();j++){
hash_used[j] = true;
length++;
}
for(int j = i-;hash_used.find(j)!=hash_used.end();j--){
hash_used[j] = true;
length++;
}
longest = max(longest,length);
}//for
return longest;
}
};
128. Longest Consecutive Sequence的更多相关文章
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【LeetCode】128. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- 128. Longest Consecutive Sequence最长连续序列
[抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- 128. Longest Consecutive Sequence (HashTable)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- kafka集群和zookeeper集群的部署,kafka的java代码示例
来自:http://doc.okbase.net/QING____/archive/19447.html 也可参考: http://blog.csdn.net/21aspnet/article/det ...
- leetcode 145. Binary Tree Postorder Traversal ----- java
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- Apache配置站点根目录、用户目录及页面访问属性
一.配置站点根目录及页面访问属性 DocumentRoot "/www/htdoc" <Directory "/www/htdoc"> Option ...
- Linux 的账号与群组[转自vbird]
Linux 的账号与群组 管理员的工作中,相当重要的一环就是『管理账号』啦!因为整个系统都是你在管理的, 并且所有一般用户的账号申请,都必须要透过你的协助才行!所以你就必须要了解一下如何管理好一个服务 ...
- (转)现代C++函数式编程
本文转自:http://geek.csdn.net/news/detail/96636 现代C++函数式编程 C++ 函数式编程 pipeline 开发经验 柯里化 阅读2127 作者简 ...
- struts2获得request和session对象
在struts1中,获得到系统的request或者session对象非常方便,都是按照形参传递的,但是在struts2中,request和session都被隐藏了 struts2提供两种方式访问ses ...
- linux free命令中buffer与cache的区别
linux free命令中buffer与cache的区别 2012-05-15 个评论 收藏 我要投稿 linux free命令中buffer与cache的区别 ~$ ...
- 怎么用ABBYY将PDF转换为JPEG图像
FineReader Mac版,全称ABBYY FineReader Pro for Mac,是一款流行的OCR图文识别软件,可快速方便地将扫描纸质文档.PDF文件和数码相机的图像转换成可编辑.可搜索 ...
- CARP-VRRP-HSRP
CARP-VRRP-HSRP http://www.openbsd.org/faq/pf/carp.html