[LeetCode] 数组的最长连续数, O(n)解法
Longest Consecutive Sequence
|
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Your algorithm should run in O(n) complexity. |
如果时间复杂度没有要求的话思路很常见,先排序O(nlogn),然后从头遍历到尾,找到最长的连续序列就可以了。
但是这里的时间复杂度要求是O(n)
实现思路需要做一些改变:我们先定义一个map<int, int>,遍历一遍数组,将(key, value)存入map,key是数组中的每一个数,value是1。
接着,我们再遍历一遍数组,对于当前遍历的某个数 k,我们定义一个值 index,index从k开始不停自增1,如果每次自增1后 index 依然可以在map中找到值,就说明数组中存在k,k+1, k+2...这样的连续序列;接着,index 从k开始不停自减1,直到map里找不到这样的index,这样就找出了k-1, k-2, ...这样的连续序列。我们将两次计算找到的连续序列总长度len存储下来。
遍历到下一个数时,依旧这样做。最后找到len的最大值。
为了避免重复便利,map中已经访问过的key可以设置为-1,当我们遍历到数组中某一个值k时,如果map[k] == -1,说明k已经被计入过某一个连续序列了,因此不用继续计算。
因此,数组所有的元素都被访问两次,总时间复杂度为O(2n)。
代码:
class Solution {
public:
int longestConsecutive(vector<int> &num) {
if(num.size() == ) return ;
map<int, int> m;
vector<int>::iterator ite = num.begin();
vector<int>::iterator end = num.end();
for(; ite != end; ++ite){
if(m.find(*ite) == m.end())
m.insert(pair<int, int>(*ite, ));
}
int maxlen = , len = ;
for(ite = num.begin(); ite != end; ++ite){
if(m[*ite] > ){
int index = *ite;
len = ;
for(; m.find(index) != m.end(); ++len, m[index++] = -);
for(index = *ite-; m.find(index) != m.end(); ++len, m[index--] = -);
if(len > maxlen) maxlen = len;
}
}
return maxlen;
}
};
[LeetCode] 数组的最长连续数, O(n)解法的更多相关文章
- 【LeetCode】128. 最长连续序列
题目 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 解释:最长连续序列是[1, 2, 3, ...
- 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- LeetCode 最长连续递增序列
给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3,5,7] 也 ...
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
- 【LeetCode】1438. 绝对差不超过限制的最长连续子数组 Longest Continuous Subarray With Absolute Diff Less Than or Equal t
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
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 ...
随机推荐
- 从wait_type入手模拟SQL Server Lock
一.LCK_M_S,等待获取共享锁 开始一SQL TRAN,其中执行对某数据的UPDATE.但并不COMMIT,也不ROLLBACK. begin tran update [dbo].[HR_Empl ...
- 使用DataTables导出html表格
去年与同事一起做一个小任务,需要把HTML表格中的数据导出到Excel.用原生js想要实现,只有IE浏览器提供导出到微软的Excel的接口,这就要求你电脑上必须安装IE浏览器.Excel,而且必须修改 ...
- javaIO--文件操作类
文件操作类主要是使用File类的各种方法对文件和目录进行操作.包括文件名.文件长度.最后修改时间和是否只读等,提供获得当前文件的路径名.判断文件是否存在.创建.删除文件和目录等一系列的操作方法. 下面 ...
- JSP传递数组给JS的方法
由于JSP页面的数组无法直接传到JS.所以采用以下方法来获取数组. <% String[] title = { "姓名 ", "学号 ", "性 ...
- web前端之路 - 开篇
一 web发展历程 了解事物的历史有助于我们渐进式的从发展的思路清楚了解事物的来龙去脉. 这里有一篇网文写得比较清晰和完整:https://www.tianmaying.com/tutorial/we ...
- 可用于jquery animate()方法的css属性
* backgroundPosition * borderWidth * borderBottomWidth * borderLeftWidth * borderRightWidth * border ...
- open-stf 安装篇(linux)
OpenSTF 百度MTC的远程真机调试 Testin的云真机 腾讯WeTest的云真机 阿里MQC的远程真机租用 什么是OpenSTF? OpenSTF是一个手机设备管理平台,可以对手机进行远 ...
- arc076 F - Exhausted? (霍尔定理学习)
题目链接 Problem Statement There are M chairs arranged in a line. The coordinate of the i-th chair ($$$1 ...
- Socket网络编程实例2
两个程序通过“网络”交互数据就使用socket,它只负责两件事:建立连接,传递数据. 所有的数据传输接收,必须都使用byte格式 1.简单实例: #客户端 import socket client=s ...
- [LouguT30212]玩游戏
题面在这里 description 对于\(k=1,2,...,t\),求\[\frac{1}{nm}\sum_{i=1}^{n}\sum_{j=1}^{m}(a_i+b_j)^k\] 对\(9982 ...