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 ...
随机推荐
- 使用python获取知乎**话题下的所有回答,并统计后发布。
第一步:获取话题需要的url需要,并向上取整 for idx in range(0,math.ceil(totals/5)): url = f"https://www.zhihu.com/a ...
- 力扣1083(MySQL)-销售分析Ⅲ(简单)
题目: Table: Product Table: Sales 编写一个SQL查询,报告2019年春季才售出的产品.即仅在2019-01-01至2019-03-31(含)之间出售的商品. 以 任意顺序 ...
- portainer安装,配置,使用
Portainer安装 Portainer是Docker容器管理可视化界面,主要是可以通过可视化界面创建,管理Dockert容器,并且支持多个节点管理(免费版支持五个节点). Portainer官网地 ...
- 提质增效,安全灵活,阿里云EDA上云方案让芯片设计驶入高速路
简介: 今天下午14点,直播间等你 导语:随着芯片工艺的跃升,EDA 需要越来越大的计算能力,处理高达PB级的海量数据.传统的算力交付模式已无法跟上快速发展的芯片设计行业,云的快速交付与强大生态提供了 ...
- 科普达人丨漫画图解什么是eRDMA?
简介: 绕过CPU,将数据直接从一台计算机的内存传输到另一台计算机,进行网络加速 在一个领先的阿里云数据中心里,数百台服务器(也就是大型的计算机)在疯狂工作和通信,他们正在合力完成一个大型的大数据处理 ...
- 全方位事件监控管理,阿里云日志服务Kubernetes事件中心正式上线
2020年2月21日,阿里云日志服务Kubernetes事件中心正式上线,为Kubernetes事件提供集中化采集.存储.分析.可视化.告警等能力,帮助Kubernetes使用者快速构建准实时.高可靠 ...
- CCO x Hologres:实时数仓高可用架构再次升级,双11大规模落地
简介:本文将会介绍今年是如何在去年基础上进行实时数仓高可用架构升级,并成功大规模落地双11. 作者 | 梅酱 来源 | 阿里技术公众号 一 2021年双11总结 2021年阿里巴巴双11期间,由CC ...
- 阿里云边缘云全新架构升级,助力CDN操控新体验
简介: 本次升级根据上万企业客户的使用反馈和行业应用特征,从简单开通到个性化定制,从内容分发到边缘计算完整解决方案,对客户侧的使用体验进行了全局梳理和全链路优化,推进边缘云CDN操控革新,并逐步构建 ...
- [GPT] 使用 nodejs的 puppeteer 库使用完关闭后,linux上面有很多 chrome 进程
在使用 Node.js 的 Puppeteer 库时,如果你在使用完后关闭了浏览器,但在 Linux 上仍然存在很多 Chrome 进程,可能是因为没有正确地关闭所有相关的进程. 可以尝试以下方法 ...
- [Linux] 启动管理: 运行级别
Link:https://www.cnblogs.com/farwish/p/14983932.html