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的更多相关文章

  1. LeetCode——Longest Consecutive Sequence

    LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...

  2. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  3. LeetCode: Longest Consecutive Sequence 解题报告

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  4. [leetcode]Longest Consecutive Sequence @ Python

    原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: Given an unsorted array of i ...

  5. LeetCode: Longest Consecutive Sequence [128]

    [题目] Given an unsorted array of integers, find the length of the longest consecutive elements sequen ...

  6. Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. LeetCode—Longest Consecutive Sequence

    题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  8. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [Leetcode] Longest Consecutive Sequence 略详细 (Java)

    题目参见这里 https://leetcode.com/problems/longest-consecutive-sequence/ 这个题目我感觉很难,看了半天别人写的答案,才明白个所以然.下面的代 ...

随机推荐

  1. phpDocumentor 注释语法详解

    PHPDocumentor是强大的代码注释生成器,本文对各个参数进行了简单地的总结: @abstract-------------使用@abstract标记来声明一个方法,类变量或类必须重新定义子类中 ...

  2. 9.10---堆箱子问题(CC150)

    1,牛客网的解题思路:其实这就是求一个最长的递增子序列.   以某一个箱子结尾的最大高度=放在它上面的所有类型中高度最大的那个+他自己的高度. import java.util.*; public c ...

  3. UOJ 做题记录

    UOJ 做题记录 其实我这么弱> >根本不会做题呢> > #21. [UR #1]缩进优化 其实想想还是一道非常丝播的题目呢> > 直接对于每个缩进长度统计一遍就好 ...

  4. Oauth 2.0第三方账号登录原理图

    百度.QQ等服务商

  5. C#中使用ADOMD.NET查询多维数据集

    ADOMD.NET 是用于与 Microsoft SQL Server Analysis Services 进行通信的 Microsoft .NET Framework 数据访问接口. ADOMD.N ...

  6. mongoengine

    http://docs.mongodb.org/ecosystem/drivers/python/ http://www.cnblogs.com/holbrook/archive/2012/03/11 ...

  7. 77 找出最大连续自然数个数[Longest Consecutive Sequence in an Unsorted Array]

    [本文链接] http://www.cnblogs.com/hellogiser/p/Longest-Consecutive-Sequence-in-an-Unsorted-Array.html [题 ...

  8. codeforces 425C Sereja and Two Sequences(DP)

    题意读了好久才读懂....不知道怎么翻译好~~请自便~~~ http://codeforces.com/problemset/problem/425/C 看懂之后纠结好久...不会做...仍然是看题解 ...

  9. 初次使用 VUX

    1. 因为以前没用过vux ,所以还是比较不熟练: 2.项目部署是根据git上的源码改的: 开始:将git上的项目下载或者clone到本地: 001:安装nodejs--> 002:npm in ...

  10. 【leetcode】Subsets II (middle) ☆

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...