LeetCode128 最长连续序列
最长连续序列
题目链接:LeetCode128
描述
给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。
请你设计并实现时间复杂度为 O(n) 的算法解决此问题。
示例
输入:nums = [100,4,200,1,3,2]
输出:4
解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
思路
1、用一个哈希表存储数组中的数。
2、遍历哈希表,查找当前的数前面一个数是否存在,即num-1在哈希表里面是否存在,如果存在这个数,那么当前的num肯定不是最长连续序列的第一位数,直接跳过。
3、如果不存在,那么计算以num开头的连续序列的长度,直接判断num++是否存在,存在则长度curLen++。
4、将得到的连续序列的长度与前一个连续序列的长度比较,取大的那一个,不断更新最长连续序列的长度,最后返回。
代码
class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int num : nums){
set.add(num);
}
int result = 0;
for(int num : set){
if(!set.contains(num-1)){
int curLen = 0;
int curNum = num;
while(set.contains(curNum++)){
curLen += 1;
}
result = Math.max(result,curLen);
}
}
return result;
}
}
LeetCode128 最长连续序列的更多相关文章
- [Swift]LeetCode128. 最长连续序列 | Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- [leetcode-128] 最长连续序列
给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1, 2, ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- lintcode: 最长连续序列
最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 说明 要求你的算法复杂度为O(n) 样例 给出数组[100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, 2, 3 ...
- [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [Swift]LeetCode298. 二叉树最长连续序列 $ Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- 【LeetCode】128. 最长连续序列
题目 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 解释:最长连续序列是[1, 2, 3, ...
- lintcode-124-最长连续序列
124-最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 说明 要求你的算法复杂度为O(n) 样例 给出数组[100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, ...
随机推荐
- 轻松易懂,一文告诉你什么是http协议?
阅读本文之前,请详细阅读以下几篇文章: <一文包你学会网络数据抓包> <教你如何抓取网络中的数据包!黑客必备技能> 一.什么是http? Http协议即超文本传送协议 (HTT ...
- 为何AI更懂你:向量搜索,了解一下!
现在,你有没有发现自己越来越多地依赖推荐系统,有时候自己搜到的结果好像还没有AI推荐的精准. 那估计有人好奇了,推荐系统怎么这么"聪明"的呢?答案就是:"向量搜索&quo ...
- love music
https://music.163.com/outchain/player?type=2&id=215239
- Kubernetes-4:kubectl常用命令总结
kubectl常用命令 1.kubectl get 显示资源 ## 查看pod列表,若pod后不指定名称空间(namespace),则默认为default名称空间 kubectl get pod ## ...
- 【YashanDB数据库】数据库运行正常,日志出现大量错误metadata changed
问题现象 客户在麒麟kylin v10 sp1环境上部署YashanDB,运行正常, 但是在巡检中发现run.log中有大量错误日志文件metadata changed,需要排查原因 问题单:前置机大 ...
- 【YashanDB知识库】YAS-00103 no free block in dictionary cache
[问题分类]功能使用 [关键字]YAS-00103,no free block in dictionary cache [问题描述]执行union all 太多子查询导致报错,例子如下: [问题原因分 ...
- Azure 入门系列 (第五篇 Azure Storage)
本系列 这个系列会介绍从 0 到 1 搭建一个 Web Application 的 Server. 间中还会带上一些真实开发常用的功能. 一共 6 篇 1. Virtual Machine (VM) ...
- 文件包含与PHP伪协议
文件包含与伪协议 一.无任何过滤措施的文件包含漏洞:(ctfshow-web78): 1.data://协议: ?file=data://text/plain,<?php system('tac ...
- 6How To Use Messages With Flask - Flask Fridays #6 10:43
消息闪现 消息闪现 {% for message in get_flashed_messages() %} <div class="alert alert-success alert ...
- qemu的使用
一.QEMU的运行模式 直接摘抄自己<揭秘家用路由器0day漏洞挖掘技术>,网上查了一下也没有找到令人满意的QEMU的使用说明,就采用这本书上的介绍.如果后期能够找到比较满意的QEMU的使 ...