题目链接:https://leetcode-cn.com/problems/longest-consecutive-sequence/

题目大意:

  略。

分析:

  注意有重复值,序列为空等情况。

代码如下:

 class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_map< int, int > m1; // 存以 key 为首的最长连续序列的长度
unordered_map< int, int > m2; // 存以 key 为尾的最长连续序列的长度
unordered_set< int > vis; // 检查数是否出现过
int ans = ; for(int i = ; i < nums.size(); ++i) {
if(vis.find(nums[i]) != vis.end()) continue;
vis.insert(nums[i]); m1[nums[i]] = m2[nums[i]] = ; if(m2.find(nums[i] - ) != m2.end()) { // 查一下是否存在以nums[i] - 1结尾的序列
splice(nums[i] - , nums[i], m2, m1); // 拼接
}
if(m1.find(nums[i] + ) != m1.end()) { // 查一下是否存在以nums[i] + 1开头的序列
splice(nums[i], nums[i] + , m2, m1); // 拼接
}
} for(auto &x : m1) ans = max(ans, x.second); return ans;
} // 拼接以B结尾的序列和以A开头的序列
inline void splice(int B, int A, unordered_map< int, int > &mB, unordered_map< int, int > &mA) {
mB[A + mA[A] - ] = mA[B - mB[B] + ] = mB[B] + mA[A];
mA.erase(A);
mB.erase(B);
}
};

Leetcode 最长连续序列的更多相关文章

  1. leetcode 最长连续序列 longest consecutive sequence

    转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 2 ...

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

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

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

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

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

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  5. 图解leetcode —— 128. 最长连续序列

    前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). ...

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

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

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

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

  8. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  9. [leetcode]128. Longest Consecutive Sequence最长连续序列

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

随机推荐

  1. 测开之路五十六:实现类似unittest的断言

    import inspect class Case(object): """ 实现断言 """ def __init__(self): se ...

  2. python with as 以上这段代码执行完毕后,就算在处理过程中出问题了,文件 f 总是会关闭。

    with open("myfile.txt") as f: for line in f: print(line, end="") 以上这段代码执行完毕后,就算在 ...

  3. Mycat+Pxc的配置

    1 schema.xml配置文件 Balance属性 负载均称类型 0:不开启读写分离机制,所有读操作都发送到当前可用的writeHost上 1:全部的readHost与stand by writeH ...

  4. VLAN基础配置及Access接口 实验1

    1.Access接口 是交换机上与PC机上相连的端口 2.当主机向交换机发送数据帧时,经过的Access口会将该帧加一个与自己PVID一致的VLAN标签 当交换机的Access口要发送给PC机一个带有 ...

  5. Cocos2d-x之事件处理机制

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 事件处理机制分为单点触屏,多点触屏,加速度事件,键盘事件和鼠标事件.在现在的智能手机中,触屏的应用比较的广泛,尤其是多点触屏事件的技术,使 ...

  6. web自动化,selenium 无法清空输入框默认值继续输入

    有的页面输入框自带默认值,想要修改里面的内容时,先使用clear()再send_keys(),这种方式无法清除只会在默认值后面追加内容,不是我想要的结果 解决方法: 方法一: 先双击,后直接send_ ...

  7. VS进程附加的使用

    一,附加进程(ctrl+alt+p)调试本地服务器接口(WCF,webapi等)的api 1>接口(WCF,webapi等)的api发布到本地IIS服务器,然后使用附加进程,将你发布的IIS添加 ...

  8. ollvm 新增字符串加密功能

    好久没弄ollvm了,可以继续了,今天给ollvm新增了一个pass,用来加密字符串,这个pass是从别的库里面扒出来的. 本文是基于在Windows 上使用VS2017编译出来的ollvm,在这个基 ...

  9. 【学习总结】Python-3-多个变量赋值

    菜鸟教程-Python3-基本数据类型 同时为多个变量赋值的两种格式: 连等:看起来可能错误但事实上Python可以这样的.... 一团变量对应一团值:比较常见又省事的格式 END

  10. go语言从例子开始之Example28.非阻塞通道操作

    常规的通过通道发送和接收数据是阻塞的.然而,我们可以使用带一个 default 子句的 select 来实现非阻塞 的发送.接收,甚至是非阻塞的多路 select. Example: package ...