LeetCode 128. Longest Consecutive Sequence 最长连续序列 (C++/Java)
题目:
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Your algorithm should run in O(n) complexity.
Example:
Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is[1, 2, 3, 4]. Therefore its length is 4.
分析:
给定一个未排序的整数数组,找出最长连续序列的长度。
可以先对数组进行排序,然后遍历数组,判断数字是否连续来计算最大长度,不过由于排序,时间复杂度是O(nlogn),我们可以利用哈希表来存储数组元素,再遍历元素,当前元素为num时,如果num-1这个元素不在我们的集合中,就代表这个num可以作为序列的起始点,然后依次判断num++是否在集合中,更新当前序列最大长度,当出现num++不在集合中,也就是此时序列不再连续,更新全局最大长度,继续遍历数组,最后返回全局的最大长度即可。
程序:
C++
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> set(nums.begin(), nums.end());
int res = 0;
for(int num:nums){
if(!set.count(num-1)){
int l = 1;
while(set.count(++num)){
l++;
}
res = max(res, l);
}
}
return res;
}
};
Java
class Solution {
public int longestConsecutive(int[] nums) {
if(nums.length == 0)
return 0;
int res = 0;
Set<Integer> set = new HashSet<>();
for(int num:nums)
set.add(num);
for(int num:nums){
if(!set.contains(num-1)){
int l = 1;
while(set.contains(++num))
l++;
res = Math.max(l, res);
}
}
return res;
}
}
LeetCode 128. Longest Consecutive Sequence 最长连续序列 (C++/Java)的更多相关文章
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- 128. Longest Consecutive Sequence最长连续序列
[抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- 298. Binary Tree Longest Consecutive Sequence最长连续序列
[抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
- [LeetCode] 128. Longest Consecutive Sequence 解题思路
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- leetcode 128. Longest Consecutive Sequence ----- java
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Leetcode 128. Longest Consecutive Sequence (union find)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
随机推荐
- 百万TPS高吞吐、秒级低延迟,阿里搜索离线平台如何实现?
导读:阿里主搜(淘宝天猫搜索)是搜索离线平台非常重要的一个业务,具有数据量大.一对多的表很多.源表的总数多和热点数据等特性.对于将主搜这种逻辑复杂的大数据量应用迁移到搜索离线平台总是不缺少性能的挑战, ...
- EMR StarRocks 极速数据湖分析原理解析
简介:数据湖概念日益火热,本文由阿里云开源大数据 OLAP 团队和 StarRocks 数据湖分析团队共同为大家介绍" StarRocks 极速数据湖分析 "背后的原理. [首月9 ...
- KubeVela v1.2 发布:你要的图形化操作控制台 VelaUX 终于来了!
简介:时间来到 2022 年,KubeVela 也正式进入了第四个阶段,在原先核心控制器 API 基本稳定的基础上,我们以插件的形式增加了一系列开箱即用的功能.让开发者可以通过 UI 控制台的方式, ...
- hyengine - 面向移动端的高性能通用编译/解释引擎
简介:手机淘宝客户端在历史上接过多种多样的脚本引擎,用于支持的语言包括:js/python/wasm/lua,其中js引擎接过的就有:javascriptcore/duktape/v8/quickj ...
- Hologres如何支持超高基数UV计算(基于roaringbitmap实现)
简介: 本文将会介绍Hologres基于roaringbitmap实现超高基数的UV计算 RoaringBitmap是一种压缩位图索引,RoaringBitmap自身的数据压缩和去重特性十分适合对于大 ...
- Nacos 2.0 升级前后性能对比压测
简介: Nacos 2.0 通过升级通信协议和框架.数据模型的方式将性能提升了约 10 倍,解决继 Nacos 1.0 发布逐步暴露的性能问题.本文通过压测 Nacos 1.0,Nacos 1.0 升 ...
- 数据改变后更新UI(SetProperty/RaiseCanExecuteChanged)
View代码 1 <StackPanel> 2 <TextBlock Text="方法一"></TextBlock> 3 <TextBox ...
- Raft 共识算法1-Raft基础
Raft 共识算法1-Raft基础 Raft算法中译版地址:http://www.redisant.cn/etcd/contact 英原论文地址:https://raft.github.io/raft ...
- 一篇文章让你掌握99%的Python运算符。干货很多,建议收藏!!!
Python 中的运算符是编程中的基础概念,用于执行各种操作和数据计算.以下是一些 Python 中的主要运算符的概述: 运算符 1. 算术运算符 算术运算符语法规则 +:加法 -:减法 *:乘法 / ...
- Python第三方库的安装和导入
目录 一.Python第三方库的安装 1. 使用pip命令行安装 2. 使用PyCharm进行安装 3. 下载第三方库文件到本地进行安装 4. 通过国内源进行安装 二.Python第三方库的导入 1. ...