[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 ...
随机推荐
- poj 1012 Joseph (约瑟夫问题)
Joseph Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 47657 Accepted: 17949 Descript ...
- Configuring Network Configuration-RHEL7
1.查看网络状态systemctl status NetworkManager You can use the systemctl status NetworkManager command to ...
- Linux 系统运行级别
Linux运行级别从0-6,共7个. 0:关机.不能将系统缺省运行级别设置为0,否则无法启动. 1:单用户模式,只允许root用户对系统进行维护. 2:多用户模式,但不能使用NFS(相当于Win ...
- 布局动画 LayoutAnimation
简介 http://blog.csdn.net/pipisky2006/article/details/8317091 补间动画只能对一个控件使用,如果要对某一组控件播放一样的动画的话,可以考虑lay ...
- PHP 单链表
<?php class Hero { public $no; public $name; public $nickname; public $next=null; public function ...
- 解压版mysql安装--windows系统
1 解压到某个目录 2 配置配置文件 3 执行命令:安装目录/bin/mysqld --install mysql5.6 --defaults-file=指定配置文件位置 "安装目录/bin ...
- 【转】深入理解Java内存模型(二)——重排序
数据依赖性 如果两个操作访问同一个变量,且这两个操作中有一个为写操作,此时这两个操作之间就存在数据依赖性.数据依赖分下列三种类型: 名称 代码示例 说明 写后读 a = 1;b = a; 写一个变量之 ...
- Eclipse换背景
http://tieba.baidu.com/p/2128040173 1.打开Eclipse的Help->Eclipse Marketplace 2.在Find里搜索Eclipse Color ...
- tomcat加入系统服务
在实际的项目开发中web容器等都是安装在客户方的服务器上的,在实现tomcat的集群时为了实现防止客户重启的机器造成服务器的关闭因此需要将web容器加入到系统服务中,在系统启动的时候自动启动服务,以t ...
- 转:测试计划(出处:: 51Testing软件测试网--zfx081)
测试计划阶段主要处于测试的先期准备阶段,在该阶段中主要是对将要进行的测试工作做一个整体的规划.包括一下内容: 1.测试目的和测试项目简介. 1.1测试目的:××××系统的测试计划有助于实现一下目标 ...