题目链接: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. 家用NAS配置方案

    对家用用户而言,NAS即一台下载机,硬件需要满足以下几点: 1.稳定性:24×7稳定无故障运行. 2.拓展性:较多的硬盘槽位,便于容量扩容: 3.体积小巧:占地面积小,便于放置. 4.方便远程管理:无 ...

  2. HttpServletRequest 对文件上传的支持

    此前,对于处理上传文件的操作一直是让开发者头疼的问题,因为 Servlet 本身没有对此提供直接的支持,需要使用第三方框架来实现,而且使用起来也不够简单.Servlet 3.0 已经提供了这个功能,而 ...

  3. 拾遗:vim 配置(个人适用,仅供参考)

    ~/.vimrc "===================通用配置====================== set encoding=utf- set statusline=%F%=[L ...

  4. 数组中重复的数字(js实现)

    题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为7的 ...

  5. 50-python基础-python3-列表-函数sorted() 对列表进行临时排序

    sorted()函数对列表进行临时排序,返回排序后的列表: 区别列表方法sort()原地修改,无返回值. 1-要保留列表元素原来的排列顺序,同时以特定的顺序呈现它们,可使用函数sorted() . 2 ...

  6. html中代替空格、大于号、小于号等字符编码

    数字表示法的不方便之处,在于必须知道每个字符的码点,很难记忆.为了能够快速输入,HTML 为一些特殊字符,规定了容易记忆的名字,允许通过名字来表示它们,这称为实体表示法(entity). 实体的写法是 ...

  7. Codeforces 1197E Culture Code DP

    题意:你有n个俄罗斯套娃,已知每个套娃的容积和体积,问有多少个子集满足以下条件: 1:这个子集是一个极大子集,即不能再添加其它的套娃到这个子集里. 2:子集的套娃之间的间隙和最小. 思路1:线段树优化 ...

  8. vscode 常用的插件

    这些是本人在使用vscode中用的比较爽的插件,个人爱好习惯不同,请按需拿取.先声明本人是一个前端,所用的,插件都是和前端匹配的,后台的同学可以不用浪费时间了 基础插件 chinese 英文是所有读书 ...

  9. window杀死端口

    获取端口的pid:netstat  -aon|findstr "8382" 杀死pid : taskkill /pid [] -t -f

  10. spring框架的一些测试思路

    一.Spring Boot Actuators Spring Boot Actuator是Spring Boot提供的对应用系统的监控和管理的集成功能,可以查看应用配置的详细信息,例如自动化配置信息. ...