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 ...
随机推荐
- exception 打印出异常栈踪迹
Java异常抛出使用e.printStackTrace(),打印出抛出的异常栈踪迹, 如果你在catch中继续抛出这个异常,那么e.printStackTrace()也能跟踪到抛出异常的地方, 使用t ...
- hive sql基础了解
会有些不一样 1 例如使用SQL 之前,要了解用了那个库,use jz_daojia 2 使用GET_JSON_OBJECT 函数等,以及参数 匹配 $.childBrithDay 挺有意思的.新玩意 ...
- 怒转一波,此人整理的Flink特别好
Apache Flink:特性.概念.组件栈.架构及原理分析 Apache Flink是一个面向分布式数据流处理和批量数据处理的开源计算平台,它能够基于同一个Flink运行时(Flink Runtim ...
- git 查看文件修改
查看某个文件的修改历史: 用git log -p filename. git blame filename是查看目前的每一行是哪个提交最后改动的. 查看某次提交修改列表: git show 版本号 ...
- elasticsearch 关联单词查询以及Shingles
Shingle Token Filter A token filter of type shingle that constructs shingles (token n-grams) from a ...
- NoWarningNoError(第八组)----Krad项目报告
Alpha阶段展示及总结 Github地址:https://github.com/NiceKingWei/krad 项目地址:119.29.32.204/krad.html 一.项目概况 本组的项目为 ...
- Git--将已有的项目添加到github(转)
转自:https://blog.csdn.net/north1989/article/details/53471439 1. 目标: 把本地已经存在的项目,推送到github服务端,实现共享. 2. ...
- HashMap不能使用基本数据类型作为key
HashMap存储元素采用的是hash表存储数据,每存储一个对象的时候,都会调用其hashCode()方法,算出其hash值,如果相同,则认为是相同的数据,直接不存储,如果hash值不同,则再调用其e ...
- OpenCV常用基本处理函数(2)图像基本操作
可以根据像素的行和列的坐标获取他的像素值.对 BGR 图像而言,返回值为 B,G,R 例如获取蓝色的像素值: img=cv2.imread('messi5.jpg')px=img[100,100]bl ...
- Laravel 事务中使用悲观锁
laravel 提供了方便快捷的数据库事务使用方式,在使用中遇到过几个容易混淆和被误导的地方,这里做个记录,希望哪里写的不对的地方各位大神指点一下 laravel 事务分为手动方式和自动方式. 但如果 ...