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 ...
随机推荐
- 【Oracle】lpad与rpad函数,从左边对字符串使用指定的字符进行填充将1变成000001
[Oracle]lpad&rpad函数,从左边对字符串使用指定的字符进行填充将1变成000001 lpad与rpad函数 顾名思义,lpad是从左边开始进行填充,rpad是从右边开始进行填充 ...
- 旧版本的centOS下载(国内-清华)
链接如下: https://mirrors.tuna.tsinghua.edu.cn/centos-vault/
- selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.的解决办法
跟着视频学习python爬取网页信息,结果出现如下问题: 在网页上搜索各种资料,最终解决该问题,所以记录一下: 1.查看自己浏览器的版本号,点击"三个点--帮助--关于Google Chro ...
- 力扣430(java)-扁平化多级双向链表(中等)
题目: 你会得到一个双链表,其中包含的节点有一个下一个指针.一个前一个指针和一个额外的 子指针 .这个子指针可能指向一个单独的双向链表,也包含这些特殊的节点.这些子列表可以有一个或多个自己的子列表,以 ...
- EventBridge 在 SaaS 企业集成领域的探索与实践
简介: 当下降本增效是各行各业的主题,而 SaaS 应用作为更快触达和服务业务场景的方式则被更多企业熟知和采用.本文将结合实际业务场景讲述在 SaaS 行业的洞察与 SaaS 集成的探索实践. 作者: ...
- 不只有 Spring,这四款 Java 基础开发框架同样值得关注! 审核中
Java 开发不只有 Spring ,今天给大家推荐几个同样优秀的 Java 基础开发框架,为日常项目开发提供更多的选择.答应我,请不要再叫我 Spring 小子了,好吗? 项目概览: Guice: ...
- CPU是什么?
在程序是怎样跑起来的这本书中我们首先被询问的一个问题是"程序是什么?它是有什么组成的?而CPU又与程序有什么关系呢?",若我们能知道前两个,其实更容易将你带入讨论"CPU ...
- Linux备忘手册
资料来源:技术胖 jspang.com 下载linux学习路径:https://newimg.jspang.com/linux-image01.png Linux备忘手册: 百度网盘 链接:https ...
- kube-proxy 流量流转方式
简介 kube-proxy 是 Kubernetes 集群中负责服务发现和负载均衡的组件之一.它是一个网络代理,运行在每个节点上, 用于 service 资源的负载均衡.它有两种模式:iptables ...
- WebKit Inside: CSS 样式表的解析
CSS 全称为层叠样式表(Cascading Style Sheet),用来定义 HTML 文件最终显示的外观. 为了理解 CSS 的加载与解析,需要对 CSS 样式表的组成,尤其是 CSS Sele ...