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. SSM框架---详细整合教程

    http://blog.csdn.net/zhshulin/article/details/37956105/

  2. Servlet初始化及处理HTTP请求

    上一篇详细介绍了与Servlet相关的几个核心的接口和类,当我们自己写Servlet类时,一般需要继承HttpServlet类,实现init().doGet().doPost()等方法.当我们写好Se ...

  3. boost::interprocess::managed_shared_memory(2)(std::string)

    #include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> #include < ...

  4. 【BZOJ4566】[Haoi2016]找相同字符 后缀数组+单调栈

    [BZOJ4566][Haoi2016]找相同字符 Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同 ...

  5. sql优化(1)

    转自:https://blog.csdn.net/jie_liang/article/details/77340905 在sql查询中为了提高查询效率,我们常常会采取一些措施对查询语句进行sql优化, ...

  6. location.assign 与 location.replace的区别

    window.location.assign(url) : 加载 URL 指定的新的 HTML 文档. 就相当于一个链接,跳转到指定的url,当前页面会转为新页面内容,可以点击后退返回上一个页面. w ...

  7. mac同时安装多个jdk

    DK8 GA之后,小伙伴们喜大普奔,纷纷跃跃欲试,想体验一下Java8的Lambda等新特性,可是目前Java企业级应用的主打版本还是JDK6, JDK7.因此,我需要在我的电脑上同时有JDK8,JD ...

  8. Spoken English Practice (I'm having whatever you're having)

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

  9. kafka 自启脚本

    每次使用的时候都要手动去启动真头痛! 解决办法,自启吧! 方法一: 方法一: /etc/rc.local中添加 文件地址记得替换掉 ,我没使用这种,发现不是每次都行,就换了第二种方法 /usr/loc ...

  10. XML 解析之 jaxp 解析器

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