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 ...
随机推荐
- 力扣744(java&python)- 寻找比目标字母大的最小字母(简单)
题目: 给你一个排序后的字符列表 letters ,列表中只包含小写英文字母.另给出一个目标字母 target,请你寻找在这一有序列表里比目标字母大的最小字母. 在比较时,字母是依序循环出现的.举个例 ...
- 基于 Mesh 的统一路由在海外业务的实践
简介:本文主要介绍我们最近在利用 Service Mesh 架构解决海外业务问题中一些实践和价值探索.我们在海外业务引入 Mesh 架构过程中,充分利用 Istio 的基于 yaml 来描述和定义路 ...
- 谈AK管理之基础篇 - 如何进行访问密钥的全生命周期管理?
简介: 我们也常有听说例如AK被外部攻击者恶意获取,或者员工无心从github泄露的案例,最终导致安全事故或生产事故的发生.AK的应用场景极为广泛,因此做好AK的管理和治理就尤为重要了.本文将通过两种 ...
- Flink 在唯品会的实践
简介: Flink 在唯品会的容器化实践应用以及产品化经验. 唯品会自 2017 年开始基于 k8s 深入打造高性能.稳定.可靠.易用的实时计算平台,支持唯品会内部业务在平时以及大促的平稳运行.现平台 ...
- 一图速览 | DTCC 2021大会,阿里云数据库技术大咖都聊了些什么?
简介: 3天9场干货分享,快来收藏吧! 10月18日~10月20日, 由国内知名IT技术社区主办的数据库技术交流盛会--DTCC 2021 (第十一届中国数据库技术大会)在京圆满落幕.大会以&quo ...
- [Go] Golang defer 与 MySQL 连接关闭的陷阱 (database is closed)
在 golang 某些 orm 中,你经常会看到这种用法: func main() { db, err := gorm.Open("sqlite3", "test.db& ...
- LLM实战:LLM微调加速神器-Unsloth + LLama3
1. 背景 五一结束后,本qiang~又投入了LLM的技术海洋中,本期将给大家带来LLM微调神器:Unsloth. 正如Unsloth官方的对外宣贯:Easily finetune & tra ...
- 在线电子表格编辑器 Luckysheet
下载地址 不知道地址正不正经,我先用着,网上找的地址,不知道是不是可信任的,疑似官网的两个地址都打不开, https://github.com/dream-num/Luckysheet zip包 h ...
- Django性能优化:提升加载速度
title: Django性能优化:提升加载速度 date: 2024/5/20 20:16:28 updated: 2024/5/20 20:16:28 categories: 后端开发 tags: ...
- Java中CAS算法的集中体现:Atomic原子类库,你了解吗?
一.写在开头 在前面的博文中我们学习了volatile关键字,知道了它可以保证有序性和可见性,但无法保障原子性,结局原子性问题推荐使用synchronized.Lock或者AtomicInteger: ...