思路:

维护一个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. Servlet读取配置文件的三种方式

    一.利用ServletContext.getRealPath()[或getResourceAsStream()] 特点:读取应用中的任何文件.只能在web环境下. private void text3 ...

  2. 解决js 运算 精度缺失

    github地址: https://github.com/MikeMcl/big.js

  3. C++ 两款静态检查工具

    pclint(收费) http://www.gimpel.com/html/pcl.htmpc-lint是资格最老,最强力的代码检查工具,但是是收费软件,并且配置起来有一点点麻烦. ccpchecke ...

  4. sip协议呼叫流程详解

    1.SIP业务基本知识 1.1 业务介绍会话初始协议(Session Initiation Protocol)是一种信令协议,用于初始.管理和终止网络中的语音和视频会话,具体地说就是用来生成.修改和终 ...

  5. MongoDB分析工具之三:db.currentOp()

    db.currentOp() db.currentOp是个好东西,顾名思义,就是当前的操作.在mongodb中可以查看当前数据库上此刻的操作语句信息,包括insert/query/update/rem ...

  6. 性能测试之Jmeter学习(三)

    一.Jmeter执行顺序规则: 元件执行顺序的规则很简单,在同一作用域名范围内,测试计划中的元件按照如下顺序执行. (1)配置元件(config elements ) (2)前置处理程序(Per-pr ...

  7. 02-安装JDK - Java快速入门

    jdk安装的版本

  8. Laravel之简单的学生信息管理平台

    laravel框架写的简易版的学生信息管理平台,贯穿了laravel的控制器.视图.模板.模型.中间件.路由规则的使用. 页面是使用BootStrap前端框架搭建 使用laravel实现了增删改查的功 ...

  9. 用css写三角形,宽高可设置

    1.不传@h,@c === @h; 2.元素width = @w, 元素height = @h*2 3.配合上.center()实现图标居中 less版本: //上下左右居中 .center(){ p ...

  10. 删除node_modul模块

    npm安装rimraf ,npm版本号要是低于5.x.x 具体不记得了,不然就安装不了这个工具 npm install rimraf -g 然后: rimraf node_modules 在这里学到的 ...