Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

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)解法的更多相关文章

  1. 【LeetCode】128. 最长连续序列

    题目 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 解释:最长连续序列是[1, 2, 3, ...

  2. 128. Longest Consecutive Sequence *HARD* -- 寻找无序数组中最长连续序列的长度

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  3. [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  4. [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  5. LeetCode 最长连续递增序列

    给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3,5,7] 也 ...

  6. LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...

  7. 【LeetCode】1438. 绝对差不超过限制的最长连续子数组 Longest Continuous Subarray With Absolute Diff Less Than or Equal t

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...

  8. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

随机推荐

  1. @Configuration和@Bean

    @Configuration可理解为用spring的时候xml里面的标签 @Bean可理解为用spring的时候xml里面的标签 Spring Boot不是spring的加强版,所以@Configur ...

  2. 【转载】2015Android 面试题 01

    1.如何避免ANR? 答:ANR:Application Not Responding,五秒在Android中,活动管理器和窗口管理器这两个系统服务负责监视应用程序的响应. 当出现下列情况时,Andr ...

  3. JQuery常用函数方法全集

    Attribute: $("p").addClass(css中定义的样式类型); 给某个元素添加样式 $("img").attr({src:"test ...

  4. 一:yarn 介绍

        yarn的了出现主要是为了拆分jobtracker的两个核心功能:资源管理和任务监控,分别对应resouceManager(RM)和applicationManager(AM).yarn中的任 ...

  5. HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)

    Description Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “im ...

  6. 将HTML页面页脚固定在页面底部(多种方法实现)

    当一个HTML页面中含有较少的内容时,Web页面的footer部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,接下来为大家介绍下如何将页脚固定在页面底部,感兴趣的朋友可以了解下 作为一个 ...

  7. JS判断备忘

    快速引入jquery并显示重点内容 (function(d,j,s,t){t=d.body.appendChild(d.createElement("script"));t.onl ...

  8. 2018年小米高级 PHP 工程师面试题(模拟考试卷)

    1.通过哪一个函数,可以把错误转换为异常处理? A:set_error_handler B:error_reporting C:error2exception D:catch 正确答案:A 答案分析: ...

  9. ASP.NET MVC4中使用bootstrip模态框时弹不出的问题

    最近发现使用在MVC中使用bootstrip的模态框时弹不出来,但单独建立一HTML文件时可以弹出,说明代码没有问题,经过多次测试发现,在MVC的cshtml文件中添加上以下语句就能正常 @{ Lay ...

  10. SSM整合步骤

    第一步:mybatis和spring整合 mybatis-spring-1.2.2:是mybatis官方出的包: mybatis的包: mybatis和spring的整合包: spring及sprin ...