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. js之词法分析

    词法分析 词法分析: 先分析参数: 再分析变量声明: 分析函数声明: 一个函数能使用的局部变量,就从上面的3步分析而来 具体步骤: 1.函数运行前的一瞬间,生成 Active Object(活动对象) ...

  2. centos 无线网卡安装,网卡rtl8188ee

    驱动: http://www.realtek.com.tw/downloads/downloadsView.aspx?Langid=1&PNid=48&PFid=48&Leve ...

  3. matplotlib绘制直方图【柱状图】

    代码: def drawBar(): xticks = ['A', 'B', 'C', 'D', 'E']#每个柱的下标说明 gradeGroup = {'A':200,'B':250,'C':330 ...

  4. php内核和瓦力上线部署

    http://www.php-internals.com/ http://www.walle-web.io/

  5. C#之系统自带保存属性

    源代码下载链接 程序开发很多时候需要根据运行环境做不通的参数配置,通过写ini之类的文本文件是一种方法,但这种方法也同时会把数据暴露 Winform开发中可以将需要配置的字段属性保存到程序中(其实也是 ...

  6. 【python】python新手必碰到的问题---encode与decode,中文乱码[转]

    转自:http://blog.csdn.net/a921800467b/article/details/8579510 为什么会报错“UnicodeEncodeError:'ascii' codec ...

  7. Java GUI学习笔记之初识AWT和Swing

    Frame f = new Frame(); //获取显示器的尺寸 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize() ...

  8. java mail api 使用

    所需要的jar包: http://pan.baidu.com/s/1qWGZRJm 如果遇到这个错误:在windows防火墙允许 javaw.exe访问网络.或者关闭防火墙 FATAL ERROR i ...

  9. c# 获取系统版本,获取net framework 版本(Environment 类)

    1.获取当前操作系统版本信息 使用Environment.OSVersion 属性 获取包含当前平台标识符和版本号的 OperatingSystem 对象. 命名空间:  System程序集:  ms ...

  10. 常见kill信号

    字符名 数字名 组合键ctrl+ 备注 SIGTERM 15   kill的默认值,可以杀死后台进程 SIGKILL 9   不可忽略,必杀技 SIGTSTP 20 Z 前台组全暂停(只是组合键方式吧 ...