算法题丨Longest Consecutive Sequence
描述
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Your algorithm should run in O(n) complexity.
示例
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
算法分析
难度:高
分析:给定未排序的整型数组,找到数值连续的元素,并返回连续元素的最大长度。
思路:首先考虑一般的思路,可以将数组先排序,然后遍历数组元素,判断是否连续,返回最大连续元素的个数,这样的话,循环的复杂度为O (n),排序的复杂度为O (nlgn),算法的整体复杂度为O (nlgn),并不满足题目要求的复杂度。所以,该算法题目的难点是如何采用O (n)的算法。
再考虑使用哈希表来存储元素,因为哈希表提供了O (1)复杂度的Contains方法,以便我们快速的访问元素:
1. 首先,我们将数组元素构造成哈希表,并定义变量longestStreak=0,用来记录最大连续元素的个数;
2. 遍历哈希表,判断当前元素num-1,是否存在在哈希表中:
a). 如果不存在,不用处理,继续遍历哈希表下一个元素;
b). 如果存在,说明有比当前元素小1的值,则定义currentNum=当前元素,定义currentStreak=1,表示currentNum作为开始比较的元素,刚开始的连续元素个数为1;
c). 开始后续比较,如果哈希表存在currentNum+1的元素,表示当前元素currentNum有后续相邻的元素,连续的元素为之前最大连续元素次数+1,开始下个一个元素比较,即currentNum+1;
d). 后续比较结束后,将本次循环获得的currentStreak作为本次循环记录的最大连续元素个数,记录本次最大连续次数currentStreak和之前最大连续次数longestStreak的最大值到longestStreak,并进入下一个循环遍历;
3. 循环遍历结束后,返回最大连续次数longestStreak;
代码示例(C#)
public int LongestConsecutive(int[] nums)
{
var numSet = new HashSet<int>(nums);
//记录最大连续元素个数
int longestStreak = 0;
foreach (int num in numSet)
{
//存在跟当前元素连续的值
if (!numSet.Contains(num - 1))
{
int currentNum = num;
int currentStreak = 1;
//每匹配到后面连续的元素,当前最大连续元素个数+1
while (numSet.Contains(currentNum + 1))
{
currentNum += 1;
currentStreak += 1;
}
//最大连续元素个数取当前最大连续元素和记录的最大连续元素个数两者最大者
longestStreak = Math.Max(longestStreak, currentStreak);
}
}
return longestStreak;
}
复杂度
- 时间复杂度:O (n).
- 空间复杂度:O (n).
附录
算法题丨Longest Consecutive Sequence的更多相关文章
- 298. Binary Tree Longest Consecutive Sequence最长连续序列
[抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
- [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 ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- LeetCode 549. Binary Tree Longest Consecutive Sequence II
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...
- LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
随机推荐
- C#中各种计时器 Stopwatch、TimeSpan
1.使用 Stopwatch 类 (System.Diagnostics.Stopwatch)Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 St ...
- 小实验3:实现haproxy的增、删、查
# Author:Alano # -*- conding:utf-8 -*- # 这里有一个问题:为什么手动删除了haproxy_new中的内容,但是执行添加命令的时候依然显示数据已经存在? f = ...
- python爬虫--爬取某网站电影下载地址
前言:因为自己还是python世界的一名小学生,还有很多路要走,所以本文以目的为向导,达到目的即可,对于那些我自己都没弄懂的原理,不做去做过多解释,以免误人子弟,大家可以网上搜索. 友情提示:本代码用 ...
- Windows gsl runtime error的解决方案
经过两天多的奋战,终于把GLAD源码集成进来了. 在编译.链接都正确,程序开始跑之后,又出了一个让人很无奈的runtime error, 就在 /* Initialize minimizer */ T ...
- Marriage Match IV HDU - 3416
题意 给你n个点,m条边,要求每条边只能走一次的S到T的最短路径的个数 题解 在我又WA又TLE还RE时,yyb大佬告诉我说要跑两遍SPFA,还说我写的一遍SPFA是错的,然而 啪啪打脸... 而且他 ...
- 关于hibernate中hql语句 case when的写法
java hql case when的用法 if(null == sorter){ hql.append(" order by m.mDate desc,case when m.mealTi ...
- javaweb get跟post 乱码解决
get中把tomact中的servel.xml 中 content 加上 URIEncoding="UTF-8"跟 useBodyEncodingForURL="true ...
- PHPStorm 最新版 去掉参数提示 parameter name hints
最新的phpstorm有个默认开启的参数名和类型提示功能, 这对于开发有很大的帮助,但是对于有些同学来说,刚开始可鞥不是很习惯,所以就需要把他给关闭. 在 配置面板中搜索 parameter name ...
- 【翻译】《向“弹跳球”演示程序添加新功能》 in MDN
文章地址: https://developer.mozilla.org/zh-CN/docs/Learn/JavaScript/Objects/%E5%90%91%E2%80%9C%E5%BC%B9% ...
- NOIP2017 总结
联赛结束,但是我并没有得到预期的结果,特写此文分析原因,希望我不会就此退役. 回顾一年,我做了什么? 2016年联赛,我水了两天,抱着挂掉的心态拿到了1=. 2016-2017寒假,参加集训,三天考试 ...