LeetCode——Longest Consecutive Sequence

Question

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.

Subscribe to see which companies asked this question.

Hide Tags

Solution

这道题对时间复杂度要求比较高,排个序也得O(nlgn),所以想要得到O(n),那必须得用空间换时间,用上哈希表。 用哈希表的时候不要用set,因为set插入一个元素的时间复杂度是O(n),应该选择用unorder_set。

然后遍历每个数字的时候,考虑去集合中查找有没有比这个数大1或者小1的,有的话就继续查找。

我们会发现,遍历4的时候,得到的集合是1, 2, 3, 4, 遍历1的时候也是得到1, 2, 3, 4, 这就重复了呀,所以再遍历4的时候,就应该把找到的元素从集合中去掉,以免重复计算,加大了时间复杂度,具体实现如下:

class Solution {
public:
int longestConsecutive(vector<int> &num) {
unordered_set<int> table(num.begin(), num.end());
int res = 0;
for (int i : num) {
if (table.find(i) == table.end()) continue;
table.erase(i);
int pre = i - 1;
int next = i + 1;
while (table.find(pre) != table.end()) table.erase(pre--);
while (table.find(next) != table.end()) table.erase(next++);
res = max(res, next - pre - 1);
}
return res;
}
};

LeetCode——Longest Consecutive Sequence的更多相关文章

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

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

  2. LeetCode: Longest Consecutive Sequence 解题报告

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  3. [leetcode]Longest Consecutive Sequence @ Python

    原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of i ...

  4. [LeetCode] Longest Consecutive Sequence

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

  5. LeetCode: Longest Consecutive Sequence [128]

    [题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...

  6. Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明

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

  7. LeetCode—Longest Consecutive Sequence

    题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  8. [Leetcode] Longest consecutive sequence 最长连续序列

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

  9. [Leetcode] Longest Consecutive Sequence 略详细 (Java)

    题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...

随机推荐

  1. 面试之JavaWeb篇(十三)

    148,AJAX有哪些有点和缺点? 优点: 1.最大的一点是页面无刷新,用户的体验非常好. 2.使用异步方式与服务器通信,具有更加迅速的响应能力. 3.可以把以前一些服务器负担的工作转嫁到客户端,利用 ...

  2. Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the

    mysql6.0里面改成新的配置方式: hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect #old #driverClassNam ...

  3. Python之实现简单计算器功能

    一,需求分析 要求计算一串包含数字+-*/()的类似于3*( 4+ 50 )-(( 100 + 40 )*5/2- 3*2* 2/4+9)*((( 3 + 4)-4)-4)表达式的数值 二,知识点 正 ...

  4. Oracle的服务介绍以及正常运行必须启动的服务

    成功安装Oracle 11g数据库后,你会发现自己电脑运行速度会变慢,配置较低的电脑甚至出现非常卡的状况,通过禁止非必须开启的Oracle服务可以提升电脑的运行速度.那么,具体该怎么做呢? 按照win ...

  5. Oracle database精装版11gR2入门详细连接教程

    对于11g本身比较简单,适合学习者使用,对电脑要求相对较低. 自己一个人单机学习使用. 工具/原料   Oracle Database Express Edition 11g Release 2安装包 ...

  6. Spoken English Practice(I won't succumb to you, not ever again)

    绿色:连读:                  红色:略读:               蓝色:浊化:               橙色:弱读     下划线_为浊化 口语蜕变(2017/6/28) ...

  7. Android 中 js 和 原生交互

    Android中的WebView 中加载的URL 默认是在手机浏览器中加载的,我们可以覆盖这种默认的动作,让网页在WebView中打开.通过设置WebView的WebViewClent 达到这个效果. ...

  8. cocos2d 场景切换和弹出场景、收回场景

    场景弹出收回很简单 用以下代码在任意一个地方显示“设置场景”: [[CCDirector sharedDirector] pushScene:[Settings scene]]; 如果你身处“设置场景 ...

  9. XML 解析之 jaxp 解析器

    XML 的解析方式有两种方式: DOM 解析和 SAX 解析. DOM 解析: 根据 XML 的层级结构, 在内存中分配一个树形结构, 把 XML 的标签, 属性和文本都封装成对象. 优点: 可以实现 ...

  10. win7下docker配置加速器

    1.docker-machine ssh default(有时可省略) 2.sudo sed -i "s|EXTRA_ARGS='|EXTRA_ARGS='--registry-mirror ...