思路:

维护一个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的更多相关文章

  1. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  2. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  3. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

  4. 128. Longest Consecutive Sequence(leetcode)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  5. [LintCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...

  6. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  7. 24. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  8. 【leetcode】Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  9. 【LeetCode OJ】Longest Consecutive Sequence

    Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...

随机推荐

  1. 小工具之Synergy

    用于两个主机共享键盘和鼠标的工具: 软件名字:synergy软件主页: http://synergy-foss.org支持平台:linux,mac,windows 通吃 作用:通过网络在多台主机之间共 ...

  2. bzoj 3992 [SDOI2015] 序列统计 —— NTT (循环卷积+快速幂)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3992 (学习NTT:https://riteme.github.io/blog/2016-8 ...

  3. dockerfile_nginx+PHP+mongo数据库_完美搭建

      基于dockerfile创建nginx+PHP+mongo数据库_完美搭建     第一步:   从git上:git clone http://git.oursdata.com/wangyue/d ...

  4. NLP | 自然语言处理 - 语言模型(Language Modeling)

    转:http://blog.csdn.net/lanxu_yy/article/details/29918015 为什么需要语言模型? 想象“语音识别”这样的场景,机器通过一定的算法将语音转换为文字, ...

  5. PICO SCOPE 3000 Series 示波器

  6. 超级简单的跨平台高性能音视频播放框架QtAv编译指南

    目录 一.了解QtAv 二.相关文章 三.下载QtAv源码 四.下载QtAv依赖库 五.设置环境变量 1.gcc设置方式 2.msvc(cl)设置方式 六.编译 七.测试 一.了解QtAv 这几天抱着 ...

  7. app自动化测试工具robotium

    robotium基于instramentation框架,可对app白盒黑盒测试,缺点是测试进程和被测进程需要在一个进程中,不能跨应用 白盒测试时,需要app源代码,在eclipse里新建android ...

  8. maven:mirrors和repository的关系区别

    原文地址:http://my.oschina.NET/sunchp/blog/100634 1 Repository(仓库) 1.1 Maven仓库主要有2种: remote repository:相 ...

  9. [Xcode 实际操作]八、网络与多线程-(23)多线程的同步与异步的区别

    目录:[Swift]Xcode实际操作 本文将演示线程的同步与异步的区别. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] 异步线程的运行,是没有按照顺序执行的. ...

  10. button 获取 cell

        - (void)cellBtnClicked:(id)sender event:(id)event {     NSSet *touches =[event allTouches];      ...