[LeetCode] 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.
这题自己只做出个逗比版,用位向量来先标记一遍,然后在遍历位向量算出最长,这相当于是排序,不过空间消耗可能很大。。。因为位向量的大小是数组里最大那个元素,这个方法不能处理很大元素的情况,如果数组有个元素是100000,但是数组大小只是5,位向量依然要100000这么大。。。而且也不能处理负数的情况,所以是逗比版。
正常的解法就是所谓的空间换时间,用哈希表先把数组存下来耗时O(n),然后去遍历哈希表,拿出一个数然后把他升序和降序的连续数找出来移除并记录下他们的长度,然后与最大值比较并更新,这样当整个哈希表为空的时候最大值也找到了,复杂度亦是O(n)
int consecutive(unordered_set<int>& set, int value, bool asc) {
int cnt = ;
while (set.find(value) != set.end()) {
cnt++;
set.erase(value);
if (asc) {
value++;
}else {
value--;
}
}
return cnt;
} int longestConsecutive(vector<int> &num) {
int max = ;
unordered_set<int> set;
for (int n: num) {
set.insert(n);
}
for (int i = ; i < num.size(); i++) {
int value = num[i];
int seq = consecutive(set, value, true) + consecutive(set, value-, false);
if (seq > max) max = seq;
}
return max;
}
另附逗比版...
int longestConsecutive_kidding(vector<int> &num) {
int max = ;
for (int i=; i<num.size(); i++) {
if (max < num[i]) max = num[i];
}
vector<int> pos(max); for (int i = ; i < max; i++) {
pos[i] = ;
} for (int i = ; i < num.size(); i++) {
pos[num[i]] = ;
} int j = , l = ;
for (int i = ; i < max; i++) {
if (pos[i] == ) {
j++;
if (j > l) l = j;
}
else {
j = ;
}
} return l;
}
逗比
[LeetCode] Longest Consecutive Sequence的更多相关文章
- LeetCode——Longest Consecutive Sequence
LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode: Longest Consecutive Sequence 解题报告
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- [leetcode]Longest Consecutive Sequence @ Python
原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of i ...
- LeetCode: Longest Consecutive Sequence [128]
[题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...
- Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode—Longest Consecutive Sequence
题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [Leetcode] Longest Consecutive Sequence 略详细 (Java)
题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- BZOJ 4455: [Zjoi2016]小星星
Sol 容斥原理+树形DP. 这道题用的容斥思想非常妙啊!主要的思路就是让所有点与S集合中的点对应,可以重复对应,并且可以不用对应完全(意思是是S的子集也可以).这样他有未对应完全的,那就减去,从全都 ...
- git 使用入门篇
最近准备给同事培训git,发现了一个不错的资源,在这里:http://www.gitguys.com/topics/creating-a-shared-repository-users-sharing ...
- ASP.NET 上的 Async/Await 简介
原文链接 大多数有关 async/await 的在线资源假定您正在开发客户端应用程序,但在服务器上有 async 的位置吗?可以非常肯定地回答“有”.本文是对 ASP.NET 上异步请求的概念性概述, ...
- sizeof和strlen的区别
一.sizeof sizeof(...)是运算符,而不是一个函数. sizeof操作符的结果类型是size_t,在头文件中typedef为unsigned int,其值在编译时即计算好了, ...
- 如何在maven中添加本地jar包
mvn install:install-file -DgroupId=mytest-DartifactId=test-Dversion=1.1 -Dpackaging=jar -Dfile=d:\te ...
- 通过nsenter连接docker容器
通常连接Docker容器并与其进行交互有四种方法.详情见:https://github.com/berresch/Docker-Enter-Demo,下面摘录nsenter连接的方式. 查看是否安装n ...
- iOS UILocalNotification 每2周,每两个月提醒
iOS 的UILocalNotification提醒提供了默认的重复频率,比如,一天,一个星期等等,但是对于非标准的频率,比如每,2周,每2个月,无法重复提醒. 我们的思路是在应用程序开始时,把即将发 ...
- PHP中文字符串编码转换
2016年2月26日 16:47:13 星期五 情景: PHP从csv导入数据时乱码 $name = mb_convert_encoding($name, 'UTF-8', 'ASCII,GBK,GB ...
- ACM/ICPC 之 最长公共子序列计数及其回溯算法(51Nod-1006(最长公共子序列))
这道题被51Nod定为基础题(这要求有点高啊),我感觉应该可以算作一级或者二级题目,主要原因不是动态规划的状态转移方程的问题,而是需要理解最后的回溯算法. 题目大意:找到两个字符串中最长的子序列,子序 ...
- oracle触发器加条件判断
oracle触发器加条件判断,如果某个字段,isnode=0,那么不执行下面的方法,数据如下: create or replace trigger tr_basestation_insert_emp ...