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.

问题:给定一个无序数组,找出最长的连续序列,要求时间复杂度为 O(n) 。

一开始想到的是用先排序,再找结果,但是时间复杂度要求 O(n) ,使用排序会超时。

思索未果,再网上找到一个方案,借助 unordered_set 来实现。

将元素全部塞进 unordered_set 中。

取出 unordered_set 中的一个剩余元素 x ,找到 unordered_set 中 x 的全部前后相邻元素,并将 x 和相邻元素全部移除,此时能得到 x 的相邻长度。若 unordered_set 还有元素,则继续当前步骤。

在第二步中的所有相邻长度中,找出最大值便是原问题的解。

由于 unordered_set 是基于 hash_table 来实现的,所以每次插入、查找、删除都是 O(1),而全部元素只会 插入、查找、删除 1 次,所以整体复杂度是 O(n)。

     int longestConsecutive(vector<int>& nums) {

         unordered_set<int> theSet;
for (int i = ; i < nums.size(); i++) {
theSet.insert(nums[i]);
} int longest = ;
while (theSet.size() > ) { int tmp = *theSet.begin();
theSet.erase(tmp); int cnt = ; int tmpR = tmp + ;
while (theSet.count(tmpR)) {
cnt++;
theSet.erase(tmpR);
tmpR++;
} int tmpL = tmp - ;
while (theSet.count(tmpL)) {
cnt++;
theSet.erase(tmpL);
tmpL--;
} longest = max( longest, cnt); } return longest;
}

参考资料:

[LeetCode] Longest Consecutive Sequence, 喜刷刷

[LeetCode] 128. Longest Consecutive Sequence 解题思路的更多相关文章

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

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

  2. Java for LeetCode 128 Longest Consecutive Sequence

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

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

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

  4. leetcode 128. Longest Consecutive Sequence ----- java

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

  5. Leetcode 128. Longest Consecutive Sequence (union find)

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

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

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

  7. Leetcode#128 Longest Consecutive Sequence

    原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...

  8. 128. Longest Consecutive Sequence(leetcode)

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

  9. 【LeetCode】128. Longest Consecutive Sequence

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

随机推荐

  1. js类封装

    将js方法封装成类,好处就是团队开发中避免命名冲突,部分类整理代码如下: function LocalStorageHelper() { //检测浏览器是否支持localStorage this.ch ...

  2. GridView、Repeater获取当前行号

    GridView: <%# Container.DataItemIndex+1 %> Repeater:<%# Container.ItemIndex+1%>

  3. Lesson 6: Exploring the World of Typefaces

    Lesson 6: Exploring the World of Typefaces 这课提到的字体都是 英文 的. Article 1: More Google Web Fonts That Don ...

  4. 传输层-TCP

    UDP协议提供了端到端之间的通讯,应用程序只需要在系统中监听一个端口,便可以进行网络通讯.随着计算机网络的发展,计算机网络所承载的业务越来越多,有些业务数据的传输需要具备可靠性,譬如我们在进行在线聊天 ...

  5. 基于AFNetworking3.0的网络封装

    1.创建名为HTTPMethod(自己随便起名字)的头文件 2.导入AFNetworking头文件(在github上下载最新版): #import "AFNetworking.h" ...

  6. iOS开源 框架

    UI界面类项目: Panoramagl ——720全景展示 Panorama viewer library foriPhone, iPad and iPod touch MBProgressHUD — ...

  7. MySQL单列索引和组合索引的区别介绍

    MySQL单列索引和组合索引的区别介绍 作者:佚名出处:IT专家网2010-11-22 13:05 MySQL单列索引是我们使用MySQL数据库中经常会见到的,MySQL单列索引和组合索引的区别可能有 ...

  8. 使用微软 AppFabric 遇到问题

    我做的一个项目用了,但是遇到很奇怪的问题,在测试环境下,两台机做集群,一切正常,达到设计要求,但是部署到专用网络(内部网络,无法访问internet),老是提示访问服务器超时,初步排查,发现貌似是域的 ...

  9. 【转】基于jquery的无刷新表格分页

    效果图 css样式 <style> html,body{margin: 0;padding:0} a:focus {outline: none;} /* 通用表格显示 */ table, ...

  10. Autofact 的使用

    资料: http://www.cnblogs.com/linhan/p/4298971.html --其他博友 http://autofac.org/# --官网 http://efmvc.codep ...