leetcode 之Longest Consecutive Sequence(六)

这题要仔细体会下哈希表的用法,要注意的是数组本身是无序的,因此需要向左右进行扩张。
另外这个思路可以进行聚类,把连续的标记为一类。
int longestConsecutive(const vector<int> &num)
{
unordered_map<int, bool> used;
for (auto i : num)used[i] = false;
int longest = ;
for (auto i : num)
{
if (used[i])continue; int length = ;
used[i] = true;
for (int j = i + ; used.find(j) != used.end(); j++)
{
used[j] = true;
length++;
}
for (int j = i - ; used.find(j) != used.end(); j--)
{
used[j] = true;
length++;
} longest = max(longest, length); } return longest;
}
leetcode 之Longest Consecutive Sequence(六)的更多相关文章
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 128. 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 ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [Leetcode][JAVA] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- Leetcode 128. Longest Consecutive Sequence (union find)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
随机推荐
- Unity3D实现3D立体游戏原理及过程
Unity3D实现3D立体游戏原理及过程 183 0 0 下面的教程是我今天整理的资料,教大家一步步完成自己的3D立体游戏,并向大家介绍一些3D成像的原理. 理论上,每个普通的非立体3 ...
- 洛谷P1268 树的重量 【构造 + 枚举】
题目描述 树可以用来表示物种之间的进化关系.一棵"进化树"是一个带边权的树,其叶节点表示一个物种,两个叶节点之间的距离表示两个物种的差异.现在,一个重要的问题是,根据物种之间的距离 ...
- HDU 4383 To The Moon 解题报告
HDU 4383 To The Moon 题意翻译 已知一个长为\(n\)的序列\(a\),你需要进行下面的四种操作. C l r d 将区间\([l,r]\)中的数加上\(d\),同时时间加\(1\ ...
- [ACM][2018南京预赛]Lpl and Energy-saving Lamps
一.题面 样例输入: 5 4 3 10 5 2 7 10 5 1 4 8 7 2 3 6 4 7 样例输出: 4 0 1 1 3 6 5 1 5 1 2 0 3 2 4 4 3 6 5 1 二.思路 ...
- 排座位&&Little Elephant And Permutation——排列dp的处理
排列的问题,就是要把序列排个序,使之达到某种最优值或者统计方案数 dp可以解决部分排列问题. 通常的解决方案是,按照编号(优先级)排序决策,从左到右决策两种. 这里主要是第一个. 排座位• 有
- C++中添加配置文件读写方法
比如有一个工程,一些变量有可能需要不时的修改,这时候可以通过从配置文件中读取该数值,需要修改时只需要修改配位文件即可. 比如有一个这样的变量m_nTest; 我么可以写两个函数ReadConfig() ...
- 很好的c++和Python混合编程文章
c++中嵌入python入门1 本人是用vc2003+python2.5学习的,其它的也应该差不了多少 0. 坏境设置把Python的include/libs目录分别加到vc的include/lib ...
- 「Django」rest_framework学习系列-渲染器
渲染器:作用于页面,JSONRenderer只是JSON格式,BrowsableAPIRenderer有页面,.AdminRenderer页面以admin形式呈现(需要在请求地址后缀添加?fromat ...
- 高可用rabbitmq集群服务部署步骤
消息队列是非常基础的关键服务,为保证公司队列服务的高可用及负载均衡,现通过如下方式实现: RabbitMQ Cluster + Queue HA + Haproxy + Keepalived 3台ra ...
- hdu 3118 Arbiter
http://acm.hdu.edu.cn/showproblem.php?pid=3118 题意:删除最少的边使图没有奇环 二分图的定义:如果顶点能分为两个互不相交的子集,则图为二分图 二分 ...