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 ...
随机推荐
- CrossApp 0.3.1示例编译问题解决过程
1 AlertTest.h找不到 问题成因:HelloCpp工程中头文件搜索路径没有增加Classes目录,需要自己加进去.(另外由于这些文件都是在子目录中,用递归模式也行,逐个子目录添加也行) 2 ...
- struct和typedef struct的区别
当typedef与结构结合使用时,会有一些比较复杂的情况,而且在C语言和C++里面有略有差别,因此从网上摘录了一些资料. 1 首先: 在C中定义一个结构体类型要用typedef: ...
- 4.2springmvc校验
1.hibernate的校验框架validation所需要jar包: 2 在applicationContext.xml中配置校验器: <!-- 校验器 --> <bean id=& ...
- tyvj1014 - 乘法游戏 ——记忆化搜索DP
题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1014 f[i][j]表示区间[i,j]所得到的最小值. 不断地划分区间,把结果保存起来. #includ ...
- VC++ MFC橡皮筋技术
在MFC下绘制直线,使用橡皮筋技术,可以使直线效果跟随鼠标移动 //OnLButtionDown m_ptOrigin = m_ptEnd = point; //OnMouseMove ...
- WebForm 中的页面重定向和传值(转自 MSDN)
——原文地址:https://msdn.microsoft.com/zh-cn/library/6c3yckfw(v=vs.100).aspx 在开发 ASP.NET 网站时,您经常需要从一 ...
- awesome-nlp
awesome-nlp A curated list of resources dedicated to Natural Language Processing Maintainers - Keon ...
- Unity摄像机
把相机做为人物的子对象,就可以制作: 1.第1人称摄像机:把摄像机摆在眼睛前面 2.第3人称摄像机:把摄像机摆在人后上面 Clear Flags: http://www.haogongju.net/a ...
- 重力感应操控(unity iphone)
方案一:speed 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public var simulateAcceleromet ...
- 【Swift】读取文本文件字符串
var str:NSString = NSString.stringWithContentsOfFile(_srcouceFilePath,encoding:NSUTF8StringEncoding, ...