当前位置:网站首页>leetcode-128: longest continuous sequence
leetcode-128: longest continuous sequence
2022-07-31 01:44:00 【chrysanthemum bat】
题目
题目连接
给定一个未排序的整数数组 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)){
//To find the starting position of a contiguous sequence
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();
}
};
边栏推荐
- Ticmp - 更快的让应用从 MySQL 迁移到 TiDB
- 35. Reverse linked list
- Fiddler抓包模拟弱网络环境测试
- [Map and Set] LeetCode & Niu Ke exercise
- SQLserver查询最近三个月的数据,语句该怎么写sqlserver
- "Real" emotions dictionary based on the text sentiment analysis and LDA theme analysis
- 来自一位女测试工程师的内心独白...
- Analyze the capabilities and scenarios of the cloud native message flow system Apache Pulsar
- [WeChat applet] This article takes you to understand data binding, event binding, event parameter transfer, and data synchronization
- PDF split/merge
猜你喜欢
九州云获评云计算标准化优秀成员单位
数字图像隐写术之卡方分布
第一学年课程期末考试
prometheus 监控概述
VSCode Plugin: Nested Comments
Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"
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
Centos 7.9安装PostgreSQL14.4步骤
【微信小程序】一文带你了解数据绑定、事件绑定以及事件传参、数据同步
Distributed. Idempotency
随机推荐
Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化
[Map and Set] LeetCode & Niu Ke exercise
TiDB 操作实践 -- 备份与恢复
关于Redis相关内容的基础学习
16、注册中心-consul
tensorflow与GPU版本对应安装问题
简易表白小页面
一个无经验的大学毕业生,可以转行做软件测试吗?我的真实案例
1.非类型模板参数 2.模板的特化 3.继承讲解
使用docker安装mysql
kotlin中函数作为参数和函数作为返回值实例练习
The PC side determines the type of browser currently in use
Kyushu cloud as cloud computing standardization excellent member unit
Know what DTU is 4GDTU equipment
Distributed. Idempotency
System design. Short chain system design
剑指offer17---打印从1到最大的n位数
蓝牙mesh系统开发二 mesh节点开发
35. Reverse linked list
Programmer's debriefing report/summary