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 ...
随机推荐
- 同时连接gitlab和github
---恢复内容开始--- 原文地址:https://juejin.im/post/5ac0cf356fb9a028df22c246 1. 分别生成gitlab和github的ssh key 生成第一个 ...
- apt-cyg for Cygwin(setup-x86_64 .exe )在win10下的安装
cygwin安装后,如果没有选择安装所有包(这会占用5G空间,很多包不需要),再需要安装新的包,可以启动setup-x86_64 .exe(我把它放置在C:\cygwin64目录下),添加包(如wge ...
- (转)关于SimpleDateFormat安全的时间格式化线程安全问题
想必大家对SimpleDateFormat并不陌生.SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出,但如果使用不小心会导致非常微妙和难以调 ...
- 60.Median of Two Sorted Arrays(两个排序数组的中位数)
Level: Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
- IDEA compile successfully many errors still occur
Compile and install successfully with maven in IDEA, but error prompt still popup. Your local enviro ...
- 从vue的组件传值着手浅谈观察者模式
首先,提到观察者模式,这不禁让我想到了MVVM,MVVM架构模式感觉用到了观察者的思想. 我们还是按照惯例,了解一下什么是观察者模式 观察者模式,类似发布订阅模式,完成这个动作首先最少得有两个不同的对 ...
- 七、OIDC
在 OAuth 中,这些授权被称为scope. OpenID-Connect也有自己特殊的scope--openid , 它必须在第一次请求“身份鉴别服务器”(Identity Provider,简称 ...
- jQuery JCrop插件的使用详解
jQuery的一个图片剪切的一个插件, 使用插件必须条件:引入jQuery.js文件,引入jQuery.Jcrop.js文件,引入JQuery.Jcrop.css文件 1.最基本的使用方法: &l ...
- 2019牛客多校第五场F maximum clique 1 最大独立集
题意:给你n个数,现在让你选择一个数目最大的集合,使得集合中任意两个数的二进制表示至少有两位不同,问这个集合最大是多大?并且输出具体方案.保证n个数互不相同. 思路:容易发现,如果两个数不能同时在集合 ...
- https://segmentfault.com/a/1190000009892006?utm_source=tuicool&utm_medium=referral
https://segmentfault.com/a/1190000009892006?utm_source=tuicool&utm_medium=referral