博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Longest Consecutive Sequence
阅读量:2346 次
发布时间:2019-05-10

本文共 926 字,大约阅读时间需要 3 分钟。

 is:

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.

The most intuitive solution to this problem is sorting the array first, and then go through the sorted array and record the length of the longest consecutive sequence. However, the time complexity of this method is  O(nlogn) .

In order to get the  O(n)  complexity, we can pick up the first element in array(say num[0]), and then check if num[0]-1 and num[0]+1 are in the array. If yes, iteratively check the upper/lower boundary of this sequence. Until the upper/lower boundaries never change, find a new element in the array that has not been accessed and start a new search.

- See more at:

http://bo-yang.github.io/2014/07/01/longest-consecutive-sequence

转载地址:http://fbpvb.baihongyu.com/

你可能感兴趣的文章
cmake使用总结---工程主目录CMakeList文件编写
查看>>
CMake学习之路
查看>>
cmake学习笔记6-catkin的CmakeList.txt讲解
查看>>
cmake手册详解
查看>>
Maplab框架介绍(一)
查看>>
Maplab开源VI-SLAM框架介绍
查看>>
maplab(1):安装
查看>>
陀螺仪随机误差的Allan方差分析
查看>>
Ubuntu 64位安装Adobe Reader 9.5.5
查看>>
Ubuntu 下如何查看已安装的软件
查看>>
Linux 系统下可以注释标注的pdf阅读器安装、比较和推荐
查看>>
福昕阅读器foxit reader Linux版
查看>>
Ubuntu 安装百度云客户端
查看>>
每天一个linux命令:locate
查看>>
Linux 环境下载百度云资源,Firefox插件(百度网盘助手)
查看>>
ubuntu Firefox/chrome adobe flash 插件安装
查看>>
OpenCV图像变换(仿射变换与透视变换)
查看>>
仿射变换与透视变换
查看>>
Ubuntu 16.04 上安装 CUDA 9.0 详细教程
查看>>
Verify You Have a CUDA-Capable GPU
查看>>