当前位置:网站首页>leetcode-128:最长连续序列
leetcode-128:最长连续序列
2022-07-31 01:33:00 【菊头蝙蝠】
题目
题目连接
给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。
请你设计并实现时间复杂度为 O(n) 的算法解决此问题。
示例 1:
输入:nums = [100,4,200,1,3,2]
输出:4
解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
示例 2:
输入:nums = [0,3,7,2,5,8,4,6,0,1]
输出:9
解题
方法一:哈希表
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> set;
for(int num:nums) set.insert(num);//去重
int res=0;
for(int num:set){
if(!set.count(num-1)){
//为了找到一个连续序列的起始位置
int cur_num=num;
int cur_count=1;
while(set.count(cur_num+1)){
cur_num++;
cur_count++;
}
res=max(res,cur_count);
}
}
return res;
}
};
方法二:并查集
class UnionFind{
private:
vector<int> parent;
vector<int> size;
public:
UnionFind(int n){
parent.resize(n);
size.resize(n);
for(int i=0;i<n;i++){
parent[i]=i;
size[i]=1;
}
}
int find(int index){
if(parent[index]==index) return index;
return parent[index]=find(parent[index]);
}
void unite(int index1,int index2){
int p1=find(index1);
int p2=find(index2);
if(p1!=p2){
parent[p1]=p2;
size[p2]+=size[p1];
}
}
int getMaxConn(){
int res=0;
for(int i=0;i<size.size();i++){
res=max(res,size[i]);
}
return res;
}
};
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int n=nums.size();
UnionFind uf(n);
unordered_map<int,int> mp;
for(int i=0;i<n;i++){
if(mp.count(nums[i])) continue;
if(mp.count(nums[i]-1)){
uf.unite(i,mp[nums[i]-1]);
}
if(mp.count(nums[i]+1)){
uf.unite(i,mp[nums[i]+1]);
}
mp[nums[i]]=i;
}
return uf.getMaxConn();
}
};
边栏推荐
- MySQL的安装教程(嗷嗷详细,包教包会~)
- Sping.事务的传播特性
- Gateway路由的配置方式
- 程序员转正述职报告/总结
- Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
- The Meta Metaverse Division lost 2.8 billion in the second quarter, still want to continue to bet?Metaverse development has yet to see a way out
- Why use high-defense CDN when financial, government and enterprises are attacked?
- 查看zabbix-release-5.0-1.el8.noarch.rpm包内容
- Artificial Intelligence and Cloud Security
- Basic Parameters of RF Devices 1
猜你喜欢

Rocky/GNU之Zabbix部署(3)

Multiplication, DFS order

【952. Calculate the maximum component size according to the common factor】

Typescript14 - (type) of the specified parameters and return values alone

System design. Short chain system design

Ticmp - 更快的让应用从 MySQL 迁移到 TiDB

【网络安全】文件上传靶场通关(1-11关)

手把手教你配置Jenkins自动化邮件通知

The client series of the DOM series

prometheus 监控概述
随机推荐
黄东旭:TiDB的优势是什么?
GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面
MySQL——数据库的查,增,删
蓝牙mesh系统开发二 mesh节点开发
Ticmp - 更快的让应用从 MySQL 迁移到 TiDB
使用PageHelper实现分页查询(详细)
16、注册中心-consul
Know what DTU is 4GDTU equipment
勾股数元组 od js
I have been working in software testing for 3 years, how did I go from just getting started to automated testing?
计算S=a+aa+…+aa…a
Rocky/GNU之Zabbix部署(1)
ROS Action communication
蛮力法/邻接表 广度优先 有向带权图 无向带权图
Distributed. Idempotency
case语句的综合结果,你究竟会了吗?【Verilog高级教程】
Yolov7实战,实现网页端的实时目标检测
九州云入选“可信云最新评估体系及2022年通过评估企业名单”
ShardingSphere's public table combat (7)
ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!