算法题丨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 ...
- Linux显示已经挂载的分区列表
Linux显示已经挂载的分区列表 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda8 1 ...
- 芝麻HTTP:PhantomJS的安装
PhantomJS是一个无界面的.可脚本编程的WebKit浏览器引擎,它原生支持多种Web标准:DOM操作.CSS选择器.JSON.Canvas以及SVG. Selenium支持PhantomJS,这 ...
- OpenStack_I版 1.准备过程
openstack是一个开源云平台,python开发 此次部署为实验环境, 采用扁平化简单的网络架构部署 优点:低耦合的,模块化 ...
- angular路由操作
在单页面应用程序中比如angular应用,我们需要根据url的变化(即:不同的请求),来分配不同的资源.根据请求的URL来决定执行哪个模块,这个过程叫路由,同时,我们需要设计路由规则. 下面给出一个简 ...
- Windows Developer Day Review
北京时间 3 月 8 日凌晨 1 点钟,今年的第一次 Windows Developer Day 正式召开. 因为时间太晚看不了直播,我也是第二天早上在公司看的重播.整个会议过程有很多值得去研究 ...
- linux 安装沙盒virtualenv 、virtualenvwrapper
1.沙盒安装命令: 最新版本:sudo easy_install virtualenv或者sudo apt-get install virtualenv 指定版本:pip install virtua ...
- C# wav语音文件合并
开发完成语音播报产品,由于客户所使用的播放产品种类繁多,在使用HDMI接口播放音频时,由于采用的声卡不同,个别机器会出现播报声音过小,或者不播报的情况.所以采用将语音文件合并播放的方式,来解决此问题. ...
- centos svn 服务器间的数据迁移
svnadmin dump erp > ~/erp.svn 当前目录下的erp 导出到根目录下名为erp.svn tar -zcvf backupSvn.tar.gz backupSvn ...
- Luogu4137:Rmq Problem/mex
题面 传送门 Sol 这题可能是假的 离线莫队搞一搞,把数字再分块搞一搞,就行了 # include <bits/stdc++.h> # define IL inline # define ...