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 ...
随机推荐
- SAP常用命令及BASIS操作
Pfcg 角色,权限参数文件配置Su53 查看权限对象 st01 跟踪St22 看dump,以分析错误 eg.找到ABAP程序出错的地方,找出fou ...
- Mutual Training for Wannafly Union #1解题报告
---恢复内容开始--- q神等人组织的vjudge上的多校训练,题目基本上都来自于CF,#1是上周进行的,参加后感觉收获很多,因为上周准备期中比较忙,解题报告现在补上. 比赛地址(兼题目地址) A题 ...
- 扩展easyui 的表单验证 (转)
From:http://www.cnblogs.com/gengaixue/archive/2012/07/14/2591054.html easyui 的validatebox()提供了自定义验证的 ...
- Android - PopupWindow
PopupWindow就是一个可以显示在当前界面上的浮动容器,它可以用来显示任意一个View,同时还可以指定显示的位置. 下面写一个显示下拉的例子: PopupWindow mpopupWindow; ...
- 利用ftp端口设置,浅谈windows防火墙之应用+ftp直接资源管理器登陆
win服务器的版本是不错的.防火墙也比较能用.server-u 6.4则是比较稳定.兼容性好的的版本,所以很多人在用 1.服务器为了安全,一般开启windows高级防火墙,在网络连接处右键鼠标,有弹出 ...
- The Layout Process on Mac OSX and iOS
First we will recap the steps it takes to bring views on screen with Auto Layout enabled. When you’r ...
- 2016 - 1 - 24 NSURLSession (一)
一: NSURLSession简介 1.实施步骤 1.1 使用 NSURLSession对象 创建TASK ,然后执行TASK 2.TASK的类型: 二: NSURLSession的简单使用: - ( ...
- 浅谈对ECharts的使用
上个月的项目,其中有一个模块用的是ECharts来实现的,分别用了折线图,环形图,还有漏斗图,这几个都算比较常见的了,尤其是折线图,环形图,用的最多的就是它们了.之前也没怎么接触过ECharts,实际 ...
- RBL开发笔记一
从这个系列开始 陆续记录整个RBL开发的过程 废话不多说 直入主题 10:54:53 2014-08-25 今天开发任务: RBL.h 的框架搭建出来 包括RBLServer RB ...
- H5版定点投篮游戏(1)--物理模型抽象
前言: 前几天目睹了大学同学开了个微店, 算是间接体验微信公众平台的使用. 觉得非常便捷和方便, 于是自己也想捣鼓一个. 公众号取名: "木目的H5游戏世界", 定位做成一个, 个 ...