Leetcode 最长连续序列
题目链接: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 最长连续序列的更多相关文章
- leetcode 最长连续序列 longest consecutive sequence
转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 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 ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [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 ...
- 图解leetcode —— 128. 最长连续序列
前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [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 ...
- [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 ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
随机推荐
- js不同类型作比较
引用自 http://www.zhufengpeixun.cn/jishuziliao/javaScriptzhuanti/2014-06-14/300.html 不同类型间的比较,规律如下 类型 类 ...
- ucenter 整合同步登录的内部实现原理
1.用户登录discuz,通过logging.php文件中的函数uc_user_login对post过来的数据进行验证,也就是对username和password进行验证. 2.如果验证成功,将调用位 ...
- nginx的安装和负载均衡例子(RHEL/CentOS7.4)
首先安装RHEL/CentOS7.4 mini ,然后关闭防火墙和 selinux ,更新系统(参看配置linux使用本地yum安装源和Redhat7/CentOS7 关闭防火墙和 selinux两个 ...
- T1219:马走日
[题目描述] 马在中国象棋以日字形规则移动. 请编写一段程序,给定n×m大小的棋盘,以及马的初始位置(x,y),要求不能重复经过棋盘上的同一个点,计算马可以有多少途径遍历棋盘上的所有点. [输入] 第 ...
- 时间复杂度为n^2的排序
时间复杂度为n^2的排序 冒泡排序和选择排序的共同点:每次都是在找剩下元素中最小(大)的元素 不同点:冒泡排序存在多次交换,而选择排序每次只存在一次交换序号 #include<iostream& ...
- androidmanifest.xml 解码工具又来一发
背景: 最近这几天在研究facebook的协议,但是facebook的采用 SSL Pinning 技术,正常通过fiddler是不能解开SSL观察协议. 听说facebook app在 manife ...
- 定制xfce4桌面==排除appfinder的綑绑
如下等同于安装 xfce4-meta,除了不安装 app-finder emerge -avq xfwm4 xfdesktop xfce4-session xfce4-settings xfce4-t ...
- 希尔排序(shell)理论---不含源码
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 希尔排序,是一个缩小增量排序.它根据步长来进行排序,步长不同可能会产生不同的序列,但是他们的最终结果是相同的,希尔排序的官方理论难以理解, ...
- HDU 4366 Successor( DFS序+ 线段树 )
Successor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 使用批处理发布 QT 的程序
1. 将 QT Creator 生成的 exe 文件拷贝到一个目录中 :C:\Users\zyy\Desktop\qtrelease 2.新建 bat 文件:qt.bat . 编辑文件,注意替换 QT ...