最长连续序列

题目链接: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 最长连续序列的更多相关文章

  1. [Swift]LeetCode128. 最长连续序列 | Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

  2. [leetcode-128] 最长连续序列

    给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1, 2, ...

  3. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  4. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  5. lintcode: 最长连续序列

    最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 说明 要求你的算法复杂度为O(n) 样例 给出数组[100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, 2, 3 ...

  6. [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 ...

  7. [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 ...

  8. [leetcode]128. Longest Consecutive Sequence最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

  9. 【LeetCode】128. 最长连续序列

    题目 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为O(n). 示例: 输入:[100, 4, 200, 1, 3, 2] 输出:4 解释:最长连续序列是[1, 2, 3, ...

  10. lintcode-124-最长连续序列

    124-最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 说明 要求你的算法复杂度为O(n) 样例 给出数组[100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, ...

随机推荐

  1. reduce() 多种用法

    reduce()方法用于将数组简化为单一值,通过遍历数组并应用提供的函数.它可以用于求和.乘积.计算对象属性的总和.数组去重和转换数组结构等.初始值的设置会影响reduce的起始索引.不提供初始值时, ...

  2. thymeleaf学习问题整理

    使用配置 <properties> <java.version>1.8</java.version> <thymeleaf.version>3.0.9. ...

  3. sql server 将数据库表里面的数据,转为insert语句,方便小批量转移数据

    create proc [dbo].[proc_insert] (@tablename varchar(256)) as begin set nocount on declare @sqlstr va ...

  4. java 知识

    1. 单文件java 例子和简单项目例子 http://kleinfelter.com/java-hello-world-with-visual-studio-code-and-eclipse jav ...

  5. Redis常见问题和性能监控

    1 Redis常见面试问题 1.1 Redis是单线程还是多线程 Redis不同版本之间采用的线程模型是不一样的,在Redis4.0版本之前使用的是单线程模型,在4.0版本之后增加了多线程的支持. 在 ...

  6. tarjan—算法的神(一)

    本篇包含 tarjan 求强连通分量.边双连通分量.割点 部分, tarjan 求点双连通分量.桥(割边)在下一篇. 伟大的 Robert Tarjan 创造了众多被人们所熟知的算法及数据结构,最著名 ...

  7. HTML – Naming Conversion

    有些是市场的规范, 有些是我的规范 Tag Name Lower Case 参考: W3Schools Attributes Name Lower Case 参考: W3Schools Always ...

  8. Python 潮流周刊#69:是时候停止使用 Python 3.8了(摘要)

    本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...

  9. WiFi基础(四):WiFi工作原理及WiFi接入过程

    liwen01 2024.09.16 前言 802.11 无线 WiFi 网有三类帧:数据帧.管理帧.控制帧.与有线网相比,无线 WiFi 网会复杂很多.大部分应用软件开发对 WiFi 的控制帧和管理 ...

  10. 揭秘!尤雨溪成立的VoidZero如何改变前端世界

    前言 Vue和Vite之父尤雨溪宣布成立公司 VoidZero,目前已经融资3200万.这篇文章欧阳将带你了解VoidZero是如何改变javascript的世界! 关注公众号:[前端欧阳],给自己一 ...