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. Python3 Tkinter-Radionbutton

    1.创建单选按钮 from tkinter import * root=Tk() Radiobutton(root,text='b1').pack() Radiobutton(root,text='b ...

  2. 家用甲醛pm2.5温湿度传感器实验

    最近在装修房子,刷完墙漆铺完了木地板以后,屋里边有很大的味,所以就买了 攀藤科技的PMS5003ST G5ST PM2.5激光粉尘甲醛温湿度三合一传感器,打算自己测一下甲醛浓度,看看什么时候能够入住. ...

  3. RedHat/CentOS利用iso镜像做本地yum源

    在这里用iso或者光盘做本地yum源的方法是差不多的,只是用光盘的话Linux系统会自动挂载,用iso镜像的或需要手动挂载,这里就说挂载iso的方法吧. (1) 创建iso存放目录和挂载目录 mkdi ...

  4. 自测之Lesson5:标准I/O

    题目:使用perror函数和strerror函数编写一个程序. 程序代码: #include <stdio.h> #include <errno.h> #include < ...

  5. USACO 1.1.3 Friday the Thirteenth 黑色星期五

    Description 13号又是一个星期5.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至 ...

  6. 整理下本周工作中遇到的疑问;uid/euid/suid;docker镜像管理

    1.系统中的父子进程关系,以及docker是如何处理的这种父子进程关系,线上问题发现,子进程长时间得不到退出. 2.调用system系统调用发生了啥事情,发现大量的页表拷贝. 3.通过shell命令通 ...

  7. 显示系统中所有的socket信息

    netstat -aon /proc/net/tcp /proc/net/udp /proc/net/unix 相关的代码是:tcp4_seq_show(struct seq_file *file, ...

  8. matlab padarray

    功能:填充图像或填充数组 使用:B = padarray(A,padsize,padval,direction) A表示输入图像,B是填充后的图像,padsize给出了填充的行数和列数,通常用[r c ...

  9. vdbench-自动化测试脚本

    #!/usr/bin/python # -*- coding:utf8 -*- import sys import commands TEST_CONF=""" hd=d ...

  10. CentOS7 防火墙配置firewall-cmd

    firewalld(Dynamic Firewall Manager of Linux systems,Linux系统的动态防火墙管理器)服务是默认的防火墙配置管理工具. firewall-cmd 是 ...