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.

问题

给出一个未排序的整数数组,找出最长的连续元素序列的长度。

如:

给出[100, 4, 200, 1, 3, 2],

最长的连续元素序列是[1, 2, 3, 4]。返回它的长度:4。

你的算法必须有O(n)的时间复杂度 。

思路

  1. "排序转换成经典的动态规划问题"的话排序至少需要时间复杂度为O(nlog(n))——pass
  2. 利用c++中的set,直接会排序,并且没有重合的,但是set背后实现的原理牵扯到红黑树,时间复杂度不满足——pass
  3. 建立hash索引,把查找的元素周围的都访问个遍,求出个临时最大值跟全局最大值比较。当再次访问该段的元素的时候,直接跳过。这样保证时间复杂度为O(n),c++11中数据结构为unordered_set,保证查找元素的时间复杂度为O(1).

伪代码

建立无序集合existSet visitedSet分别表示原集合中包含的元素和已经访问了的元素
全局最大个数maxLen
顺序遍历原集合中的元素
临时计数count=1
如果该元素在visitedSet,停止往下执行,进行下一次循环
否则,把改元素小的并且在existSet中的元素存放在visitedSet中,count++
把改元素大的并且在existSet中的元素存放在visitedSet中, count++
maxLen = max(maxLen, count)
 class Solution {
public:
int longestConsecutive(vector<int> &num) {
int max=;
std::unordered_set<int> visit;
std::unordered_set<int> exist;
for(int i=;i<num.size();i++){
exist.insert(num[i]);
}
for(int i=;i<num.size();i++){
if(visit.find(num[i])!=visit.end()){
continue;
}
int count=;
int left=num[i]-;
while(exist.find(left)!=exist.end()){
count++;
visit.insert(left);
left--;
}
int right=num[i]+;
while(exist.find(right)!=exist.end()){
count++;
visit.insert(right);
right++;
}
if(count>max)
max=count;
}
return max;
}
};

最长连续序列(Longest Consecutive Sequence)的更多相关文章

  1. [Swift]LeetCode128. 最长连续序列 | Longest Consecutive Sequence

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

  2. leetcode 最长连续序列 longest consecutive sequence

    转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 2 ...

  3. 最长连续子序列 Longest Consecutive Sequence

    2018-11-25 16:28:09 问题描述: 问题求解: 方法一.如果不要求是线性时间的话,其实可以很直观的先排序在遍历一遍就可以得到答案,但是这里明确要求是O(n)的时间复杂度,那么就给了一个 ...

  4. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

  6. [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  7. [Swift]LeetCode298. 二叉树最长连续序列 $ Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  8. [leetcode]128. Longest Consecutive Sequence最长连续序列

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

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

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

随机推荐

  1. day05_05 for循环、break语句

    1.0 输入用户名,密码练习 _user = "alex" _passwd = "abc123" username = input("Username ...

  2. day05_03 字符串格式化

    pycharm小技巧,一般情况下都需要在代码前注释以下作者以及创建日期 但是如何让软件默认生成呢? 格式化输出 可以用占位符 %s     string的缩写 #__author:Administra ...

  3. 安装adb工包

    下载android sdk (很大) 从D:\AndroidSdk\platform-tools目录可以看到: 将adb工具包: adb.exe,AdbWinapi.dll,AdbWinUSBapi. ...

  4. 剪枝的定义&&hdu1010

    半年前在POJ上遇到过一次剪枝的题目,那时觉得剪枝好神秘...今天在网上查了半天资料,终于还是摸索到了一点知识,但是相关资料并不多,在我看来,剪枝是技巧,而不是方法,也就是说,可能一点实用的小技巧,让 ...

  5. Springmvc web项目初始化

    Web容器首先会读取项目中的web.xml配置文件中的两个节点:<context-param>与<listener> Web容器创建ServletContext对象即Servl ...

  6. [LOJ#6002]「网络流 24 题」最小路径覆盖

    [LOJ#6002]「网络流 24 题」最小路径覆盖 试题描述 给定有向图 G=(V,E).设 P 是 G 的一个简单路(顶点不相交)的集合.如果 V 中每个顶点恰好在 P 的一条路上,则称 P 是  ...

  7. iOS-跨界面传值和跨应用传值

    跨界面传值 从一个界面将一个结果值传到另一个界面,这个是我们在开发过程中非常常见的一个问题.传值本身并不是一个太复杂的问题,在此主要简述一下常用的传值方法. 我们传值常用的方法主要有四种: 1.属性传 ...

  8. 当axios需要传送application/x-www-form-urlencoded格式参数的问题

    我发送出去的数据发现后台接收不到,查找了一下原因,发现需要form-data的数据后台才可以获取到.于是改成了form-data格式,成功了,后台获取到数据了,有点小激动,但是随即发现发送的数据格式出 ...

  9. net2:DropDownList的使用

    原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  10. canvas之webgl的浏览器开启方式

    引自百度知道的回答 1.开启方式: 第一种:打开cmd,切换到Chorme的安装目录,敲入chrome.exe --enable -webgl,回车就会打开一个chrome浏览器窗口: 第二种:找到C ...