[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 ...
随机推荐
- 浅谈CPU、内存、硬盘之间的关系
计算机,大家都知道的,就是我们日常用的电脑,不管台式的还是笔记本都是计算机.那么这个看着很复杂的机器由哪些组成的呢,今天就简单的来了解一下. 先放图: 图上展示的就是计算机的基本组成啦. 首先是输入设 ...
- C语言文件基本操作
1.用文本方式储存‘1’,‘0’,‘2’存入文件,然后用二进制方式从文件开头读出一个short型数据,并验证结果是否正确 #include<stdio.h> #include<str ...
- “Hello world!”团队第三周贡献分规则
一.贡献规则制定: (1)基础分:9 , 9 , 8 , 7 , 7 , 7 , 6(按在本次编程中承担模块的重要度制定,某一模块重要度的认定通过组内开会讨论决定) (2)会议分:每人没出勤一次会议记 ...
- 1001 Duplicate Pair
1.题目戳这里 2.代码: #include<stdio.h> #include<string.h> int main() { int n; while(scanf(" ...
- <Effective C++>读书摘要--Implementations<一>
1.For the most part, coming up with appropriate definitions for your classes (and class templates) a ...
- PAT L1-048 矩阵A乘以B
https://pintia.cn/problem-sets/994805046380707840/problems/994805082313310208 给定两个矩阵A和B,要求你计算它们的乘积矩阵 ...
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction 表被锁的解决办法
转自:https://blog.csdn.net/mchdba/article/details/38313881 前言:朋友咨询我说执行简单的update语句失效,症状如下:mysql> upd ...
- Android------去除标题栏
这里暂时只给出一种方法,在java代码中去除 1.继承Activity 在onCreate方法中 getWindow().setFlags(WindowManager.LayoutParams.FLA ...
- 关联容器 // append方法
关联容器和顺序容器的差别在于:关联容器通过键(key)存储和读取元素,而顺序容器则通过元素在容器中的位置顺序存储和访问元素. 1.关联容器支持通过键来高效地查找和读取元素.两个基本的关联容器类型是ma ...
- bzoj1835[ZJOI2010]基站选址
主席树+决策单调,重写一遍比之前短多了……题解:http://www.cnblogs.com/liu-runda/p/6051422.html #include<cstdio> #incl ...