[LeetCode] 128. Longest Consecutive Sequence 解题思路
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 解题思路的更多相关文章
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Leetcode 128. Longest Consecutive Sequence (union find)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
- Leetcode#128 Longest Consecutive Sequence
原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【LeetCode】128. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
随机推荐
- [转] PostgreSQL学习手册(数据表)
from: http://www.cnblogs.com/stephen-liu74/archive/2012/04/23/2290803.html 一.表的定义: 对于任何一种关系型数据库而言,表都 ...
- C#时间戳的获取与转换方法
/// <summary> /// 获取当前时间戳 /// </summary> /// <param name="bflag"></pa ...
- Android(java)学习笔记249:ContentProvider使用之获得系统联系人信息01
1.系统联系人的数据库(3张最重要的表) (1)raw_contacts 联系人表 保存联系人的id contact_id (2)data 数据表 保存联系人的数据 ( ...
- 桶排序与基数排序代码(JAVA)
桶排序 publicstaticvoid bucketSort(int[] a,int max){ int[] buckets; if(a==null || m ...
- 自定义控件 TextView 歌词 Lrc
演示 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> < ...
- Myeclipse+Tomcat安装与配置
一: Myeclipse安装很简单,没什么可说的,下面说一下怎么把英文版的Myeclipse汉化的问题 1.把汉化包解压,将解压后的“language”文件夹,放入Myeclipse\common文件 ...
- The account '' has no team with ID ''
Xcode 升级到7.2 版本,真机测试的时候报错:The account '' has no team with ID '' 解决办法1:http://stackoverflow.com/quest ...
- jQuery旋转插件
jQuery旋转插件,支持Internet Explorer 6.0 + .Firefox 2.0.Safari 3.Opera 9.Google Chrome,高级浏览器下使用Transform,低 ...
- Sql 日期时间 转换
sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...
- java事件处理4(焦点,键盘
FocusEvent焦点事件 接口 addFocusListener(FocusListener listener) 有两个方法 public void focusGains(FocusEvent e ...