算法题丨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 ...
随机推荐
- freemarker自定义标签报错(三)
freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered " " at line 14, column ...
- ASP.NET 页面双向静态化
而我们预期的结果应该如下图,实际只请求两次. 用301重定向可以解决该循环请求产生的问题. OK, let's begin. 本文的Demo和Source是基于上一篇的,如果下面的一些文件或文件夹没有 ...
- 芝麻HTTP: Learning to Rank概述
Learning to Rank,即排序学习,简称为 L2R,它是构建排序模型的机器学习方法,在信息检索.自然语言处理.数据挖掘等场景中具有重要的作用.其达到的效果是:给定一组文档,对任意查询请求给出 ...
- 芝麻HTTP:python version 2. required,which was not found in the registry 解决方案
不能在注册表中识别python2.7 新建一个register.py 文件 import sys from _winreg import * # tweak as necessary version ...
- Django学习-16-Session
1.保存在服务器的键值对 2.Session做验证时,还要依赖Cookie(重要).当用户登录成功时,生成随机字符串,一份放到Session,一份放到Cookie.当用户再次登录, ...
- freemarker中的round、floor和ceiling数字的舍入处理(十七)
1.简易说明 (1)round:四舍五入 (2)floor:向下取整 (3)ceiling:向上取整 2.举例说明 <#--freemarker中的round.floor和ceiling数字的舍 ...
- C#图解教程 第七章 类和继承
类和继承 类继承访问继承的成员所有类都派生自object类屏蔽基类的成员基类访问使用基类的引用 虚方法和覆写方法覆写标记为override的方法覆盖其他成员类型 构造函数的执行 构造函数初始化语句类访 ...
- 【Luogu3807】【模板】卢卡斯定理(数论)
题目描述 给定\(n,m,p(1≤n,m,p≤10^5)\) 求 \(C_{n+m}^m mod p\) 保证\(P\)为\(prime\) \(C\)表示组合数. 一个测试点内包含多组数据. 输入输 ...
- 【CJOJ1494】【洛谷2756】飞行员配对方案问题
题面 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1 ...
- HiveQL DML 常用QL示例资料
hive 2.1.1 DML操作 将文件加载到hive表 //官方指导 LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tabl ...