算法题丨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 ...
随机推荐
- R语言学习 第七篇:列表
列表(List)是R中最复杂的数据类型,一般来说,列表是数据对象的有序集合,但是,列表的各个元素(item)的数据类型可以不同,每个元素的长度可以不同,是R中最灵活的数据类型.列表项可以是列表类型,因 ...
- jtyhon 介绍
Jython是Python的纯Java实现.她无缝地结合了Java类与Python,使用户能以Python语言的语法编写在Java虚拟机上运行的 软件.它的特点有:与相似的Java程序相比,Jytho ...
- tp3.2 URL_MODEL为2 配置
1. tp项目index.php同级目录 add . htaccess文件 ,rewirte重写 内容为: <IfModule mod_rewrite.c> Options +Follo ...
- [HAOI2011]problem a
题目大意: 网址:https://www.luogu.org/problemnew/show/2519 大意: 一次考试共有\(n\)个人参加, 第\(i\)个人说:"有\(a_i\)个人分 ...
- 【xsy2140】计数
Time Limit: 1000 ms Memory Limit: 256 MB description 吐槽 所以说..组合数的题是不是都是知道大致思路但是就是不会写qwq菜醒qwq 正题 这题其实 ...
- NancyFX 第七章 模型绑定和验证
任何优秀的框架,都能传递参数.在之前的路由章节,我们已经看到了如何在URL中传递参数. 能够传递简单的参数当然好,特别是在设计那些从数据库读取记录的API设计中.但是,很多情况下也是需要传递复杂对象. ...
- 分享调用Java private方法
上周在修复bug时,发现Java类中某方法是private,且类中没有用到,第一感觉是方法多余.其实通过分析,发现原来Native Code会通过JNI调到此方法.这也给自己启发,平时做Code re ...
- html学习第一弹の常用标签的归类
HTML初步学习: 行内元素:只占据他对应标签的边框所包含的空间,默认横向排布. 块级元素:块级元素占据其父元素(容器)的整个空间,因此创建了一个块,通常浏览器会在块级元素前后另起一行,默认竖向排布. ...
- C++中 #include<>与#include""
#include<> 使用尖括号表示在包含文件目录中去查找(包含目录是由用户在设置环境时设置的),而不在源文件目录去查找: #include"" 使用双引号则表示首先在 ...
- 设计模式——装饰器模式(C++实现)
#include <iostream> #include <string> using namespace std; class Component { public: ; } ...