思路:

维护一个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. Python ip与数字的转换方式

    例子:IP:192.168.1.10 方法一: In [1]: bin(192)Out[1]: '0b11000000' In [2]: bin(168)Out[2]: '0b10101000' In ...

  2. XAMPP的端口被占用

    打开xampp\apache\conf\httpd.conf文件把80端口修改为:8081;打开xampp\apache\conf\extre\httpd-ssl.conf文件把443修改为4433或 ...

  3. MakeFile 文件的使用

    什么是Makefile? 一个工程中的源文件不计其数,其按类型.功能.模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译, ...

  4. OTL之Oracle开发总结《转》

    OTL之Oracle开发总结---转   关 于OTL,网上介绍的也不少,但看来看去也只是官方的那些文档.OTL很好用,结合官方提供的一些例子,多多尝试才能领悟.经过一个月左右的项目开发,对 OTL也 ...

  5. JavaScript-Tool-导向:jquery.steps-un

    ylbtech-JavaScript-Tool-导向:jquery.steps 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 0. http://www.jqu ...

  6. sqlServer:convert()函数

    在oracle中对于时间格式的转换用到的比较多,相对也比较了解,现在换了新的项目组,使用sqlserver数据库,需要对这个数据库的一些常用函数进行重新学习和熟悉,现在根据w3c及网上博文对conve ...

  7. CodeForces 1110G. Tree-Tac-Toe

    题目简述:给定$n$个节点的树,其中一些节点被染成了白色(其余节点未染色).黑白双方博弈,白先动.轮到黑(白)方时,选择树上的一个未染色的节点并将其染成黑(白)色.率先达成三连色(即存在三个节点$a, ...

  8. 1.10-1.11 hive交互式命令讲解

    一.hive 交互式命令参数 #帮助 [root@hadoop-senior hive-0.13.1]# bin/hive -h Missing argument for option: h usag ...

  9. 1.6 Hive配置metastore

    一.配置 1.配置文件 #创建配置文件 [root@hadoop-senior ~]# cd /opt/modules/hive-0.13.1/conf/ [root@hadoop-senior co ...

  10. git 的安装使用以及协作流程

    git安装: sudo apt-get install git-core git使用: 转:https://www.liaoxuefeng.com/wiki/0013739516305929606dd ...