[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/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...
随机推荐
- HDU 4707 DFS
Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He ...
- BZOJ 4596: [Shoi2016]黑暗前的幻想乡
Sol 容斥原理+Matrix-Tree定理.容斥跟小星星那道题是一样的,然后...直接Matrix-Tree定理就可以了... 复杂度\(O(2^{n-1}n^3)\) PS:调了好久啊QAQ 明明 ...
- 35 网络相关函数(三)——live555源码阅读(四)网络
35 网络相关函数(三)——live555源码阅读(四)网络 35 网络相关函数(三)——live555源码阅读(四)网络 简介 5)NoReuse不重用地址类 6)initializeWinsock ...
- 正在使用广告标识符 (IDFA)
APP提交审核后,apple方面一直说我使用了IDFA,APP里没有集合任何广告SDK. 怀疑是其他第三方的SDK用了. 检测命令 //在项目的根目录下用终端执行 grep -r advertisin ...
- 对xml文件的简单解析
package com.eprobj.demo; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; impor ...
- 2.7---判断链表是否是回文(CC150)
注意,如果用的方法是翻转整个链表,那么链表都被改变了.就无法做了. 此外注意fast.next.next!= null;不然也会报错.要保证后面的比前面的少. 答案: public static bo ...
- PyQt4控件失去焦点和获得焦点
#QListView控件多选设置self.ui.listView.setSelectionMode(QAbstractItemView.ExtendedSelection) #初始化QListView ...
- 多节点 devstack 部署
1, 网络配置 每个节点 /etc/network/interfaces auto eth0 iface eth0 inet static address 192.168.42.11 netmask ...
- sql语句操作
1.1 SQL语句 1.1.1 什么是SQL SQL:Structured Query Language, 结构化查询语言. 特点: * 非过程性语言: * 过程性语言特点:一个语句需要依赖上面的几条 ...
- [转]Android 学习资料分享(2015 版)
转 Android 学习资料分享(2015 版) 原文地址:http://www.jianshu.com/p/874ff12a4c01 目录[-] 我是如何自学Android,资料分享(2015 版) ...