24. Longest Consecutive Sequence
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.
两种方法:1. 利用 hash_map 结构,数组有序时查找的思想。
class Solution {
public:
int longestConsecutive(vector<int> &num) {
if(num.size() == 0) return 0;
unordered_map<int, bool> _map;
for(size_t i = 0; i < num.size(); ++i)
_map.insert(pair<int ,bool>(num[i], false));
int ans = 0;
for(auto it = _map.begin(); it != _map.end(); ++it) {
if(it->second) continue; // visited
int len = 1;
int v1 = it->first-1;
while(_map.count(v1)) { _map[v1--] = true; len++; }
int v2 = it->first+1;
while(_map.count(v2)) { _map[v2++] = true; len++; }
if(len > ans) ans = len;
}
return ans;
}
};
2. 动态的构造线段(两端为线段始末位置),这样从一端点可获取另一端点位置。若当前点可增加有向线段长度,拓展线段。
class Solution {
public:
int longestConsecutive(vector<int> &num) {
int answer = 0;
unordered_map<int ,int>_map;
int low, high;
for(int i = 0; i < num.size(); ++i) {
if(_map.count(num[i])) continue; // must be used.
_map[num[i]] = num[i];
low = high = num[i];
if(_map.count(num[i]-1)) low = _map[num[i]-1];
if(_map.count(num[i]+1)) high = _map[num[i]+1];
answer = max(answer, high - low + 1);
if(low == high) continue;// not in a line
_map[low] = high;
_map[high] = low;
}
return answer;
}
};
24. Longest Consecutive Sequence的更多相关文章
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LintCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- 298. Binary Tree Longest Consecutive Sequence
题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...
随机推荐
- HDU 1561 树形DP入门
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 分析器错误 MvcApplication 找不到
<%@ Application Codebehind="Global.asax.cs" Inherits="test.MvcApplication" La ...
- php大力力 [040节] 买了一天域名,整了一天后台,新网后台不懂啊
php大力力 [040节] 买了一天域名,整了一天后台,新网后台不懂啊]]] 还有万网那些域名要备案,备案,备案中...................wqnmlgb 今天摩托车的前后轮被扎了,tnn ...
- tensorflow4
参考:tensorflow_manual_cn.pdf 一.图像的四维张量和参数的四维张量貌似不同: 二.流程回顾 1.数据准备 2.Page 63 三.状态可视化 四.保存检查点(保存参数) 五.评 ...
- RocketMQ消费者示例程序
转载请注明出处:http://www.cnblogs.com/xiaodf/ 本博客实现了一个简单的RocketMQ消费者的示例,MQ里存储的是经过Avro序列化的消息数据,程序读取数据并反序列化后, ...
- Spark SQL Thrift Server 配置 Kerberos身份认证和权限管理
转载请注明出处:http://www.cnblogs.com/xiaodf/ 之前的博客介绍了通过Kerberos + Sentry的方式实现了hive server2的身份认证和权限管理功能,本文主 ...
- linux dns 连外网
以下设置对所用的Linux系统如Redhat/Ubuntu/Debian/CentOS等都有效,但您必须是管理员root或者具有管理员权限 vim /etc/resolv.conf 在其中加入: na ...
- cocos2d Slider 透明滑动部件无法生成解决办法
用cocos studio 2.3.2 制作声音大小控制滑条的时候遇到了一个奇葩bug我把透明图片和其它资源打包到合图里面然后到到cocos stdudio里面 那张透明图片变成了只有一个像素的点,最 ...
- RPM卸载软件包
如何卸载rpm包 首先:通过 rpm -q <关键字> 可以查询到rpm包的名字 然后:调用 rpm -e <包的名字> 删除特定rpm包 如果遇到依赖,无法删除,使用 rp ...
- Boot loader: Grub进阶[转]
Boot loader: Grub进阶 本文记录grub的一些进阶配置 关於核心功能当中的 vga 配置 事实上,你的 tty1~tty6 除了 80x24 的解析度外,还能够有其他解析度的支持喔!但 ...