leetcode128 Longest Consecutive Sequence
思路:
维护一个unordered_map,key是每个连续区间的端点,value是该区间的长度。
实现:
class Solution
{
public:
int longestConsecutive(vector<int>& nums)
{
unordered_map<int, int> mp;
int ans = ;
for (auto it: nums)
{
if (mp.count(it)) continue;
int l = mp.count(it - ) ? mp[it - ] : ;
int r = mp.count(it + ) ? mp[it + ] : ;
int sum = l + r + ;
ans = max(ans, sum);
mp[it] = sum;
mp[it - l] = sum;
mp[it + r] = sum;
}
return ans;
}
};
leetcode128 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 ...
- 24. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【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 ...
随机推荐
- OCX控件避免弹出安全警告的类
1.要加一个头文件: #include <objsafe.h>2.在控件头文件中加入: 1 DECLARE_INTERFACE_MAP()2 BEGIN_INTERFACE ...
- c++ zlib(qt)压缩与解压缩
#include <QtCore/QCoreApplication> #include "zlib.h" #include "stdio.h" #i ...
- 使用 Multipath TCP 为 iOS 创建备份连接(转)
这篇文章的英文版如下: https://support.apple.com/en-us/HT201373 这里咱们采用苹果手机打开测试网站,发现没有检测到MPTCP,初步猜想可能需要打开什么设置,后续 ...
- Flutter实战视频-移动电商-18.首页_火爆专区后台接口调试
18.首页_火爆专区后台接口调试 楼层结束之后有个火爆专区.到地图有个上拉加载的效果 lib/config/service_url.dart 首先找到我们的接口配置文件,增加接口的配置 lib/ser ...
- 洛谷 - P2281 - 多项式的加法和乘法 - 大模拟
题目链接:https://www.luogu.org/problemnew/show/P2281 题目的意思很简单,输入两个系数.指数都是整数,变量都是大写字母的多项式,求他们的加法结果和乘法结果. ...
- PhpStorm插件之Api Debugger
安装插件 File->Setting->Pluugins 搜索 Api Debugger 如何使用 安装完插件后,RESTART IDE,在编辑器右侧 即可找到最新安装的 Api D ...
- ZOJ3321,ZOJ3317
ZOJ3321 //there is at most one edge between two nodes. 因为这句话的局限性,又要满足环,那么一定是每个点度为2,然后为n节点的一个环 //#inc ...
- 2013 Noip提高组 Day2
3288积木大赛 正文 题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前 ...
- rsync 同步的艺术
rsync是类unix系统下的数据镜像备份工具,可以提供快速的增量文件传输. rsync 也可用于本机传输,如: # rsync -v mysql--linux-glibc2.-x86_64.tar. ...
- 日志组件Log2Net的介绍和使用(附源码开源地址)
Log2Net是一个用于收集日志到数据库或文件的组件,支持.NET和.NetCore平台. 此组件自动收集系统的运行日志(服务器运行情况.在线人数等).异常日志.程序员还可以添加自定义日志. 该组件支 ...