[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 2124: 等差子序列
Sol 线段树+Hash. 首先暴力 等差子序列至少3项就可以了,就枚举中项,枚举公差就可以了,只需要一个数在中项前出现,另一个数在中项前没出现过就可以了.复杂度 \(O(n^2)\) 然后我想了一个 ...
- 把strassen乘法调出来了...
完美... 指针搞死我了 /// /// Author: zball /// No rights reserved /// (Creative Commons CC0) /// #include &l ...
- Javascript——Context和Scope的一些学习总结
1.1.1 摘要 在我们学习Javascript过程中,常常会遇到作用域(Scope)和执行上下文(Context)等概念.其中,执行上下文与this关键字的关系密切. 有面向对象编程经验的各位,对于 ...
- Intel项目所用jquery小知识点总结
1.$("#tdGeo input[type='checkbox']:checked") ---筛选出所有已经Check的Checkbox 2.$("#tdCount ...
- Google Code Jam 2015 R1C B
题意:给出一个键盘,按键都是大写字母.给出一个目标单词和一个长度L.最大值或者最大长度都是100.现在随机按键盘,每个按键的概率相同. 敲击出一个长度为L的序列.求该序列中目标单词最多可能出现几次,期 ...
- 如果你修改了ashx.cs的命名空间
如果你修改了ashx.cs的命名空间,记得要同时修改ashx的WebHandler的Class. 在解决方案资源管理器中右键,查看标记
- maven加载本地lib下的jar包(pom.xml)
1.将本地jar放置到仓储库在jar包目录下 mvn install:install-file -Dfile=sqljdbc4.jar -DgroupId=com.microsoft.sqlserve ...
- css3圣诞雪景球开源
css3圣诞雪景球开源 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- python基础——多重继承
python基础——多重继承 继承是面向对象编程的一个重要的方式,因为通过继承,子类就可以扩展父类的功能. 回忆一下Animal类层次的设计,假设我们要实现以下4种动物: Dog - 狗狗: Bat ...
- 点击按钮对两个div的隐藏与显示进行切换
HTML: <button type="button" id="showHidden">点击切换div的隐藏与显示</button> ...