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. FBV和CBV的差异

    FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述.   CBV CBV(class base ...

  2. [HackerRank] The Longest Common Subsequence

    This is the classic LCS problem. Since it requires you to print one longest common subsequence, just ...

  3. 支付宝app支付提示 系统繁忙,请稍后重试

    v2版本的支付宝支付,无论怎么调试返回的都是系统繁忙,请稍后重试提示.经过对给的示例代码的仔细研究,最后发现是由于老版本的sign_type 不加入签名计算,而新版本的支付签名它是加入签名计算的.供大 ...

  4. C# Remoting双向通信

    闲来无事想玩玩双向通信,实现类似QQ的互发消息的功能.于是乎开始学习.Net Remoting. .Net Remoting 是由客户端通过Remoting,访问通道以获得服务端对象,再通过代理解析为 ...

  5. Powershell Get-ChildItem 筛选文件,文件处理

    使用Where-Object也可以根据其它属性来过滤. Dir | Where-Object { $_.CreationTime -gt [datetime]::Parse("May 12, ...

  6. 克隆DOM元素 ele.cloneNode();

    var nodePrev = p.children[j].cloneNode(true); var nodeNext = p.children[j + 1].cloneNode(true);

  7. 让Windows Server 2008+IIS 7+ASP.NET支持10万个同时请求

    具体设置如下: 1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools >Advanced Settin ...

  8. TCP原理

    1.http://coolshell.cn/articles/11564.html 2.http://coolshell.cn/articles/11609.html 3.一站式学习wireshark ...

  9. PostgreSQL: WITH Queries (Common Table Expressions)

    WITH 允许在 SELECT 语句中定义"表"的表达式,这个"表"的表达式称之为"公共表表达式(Common Table Expression)&q ...

  10. Django web 框架

    目录 与Django的第一次见面 安装.文件解释与基本命令 Settings Models Views 路由系统 模板 Form表单 Cookie与Session CSRF防护