最长连续序列

题目链接: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. STM32F3, STM32F4编程手册

    1. Cortex-M4的内核设备 NVIC, Nested vectored interrupt controller SCB, System control block SysTick, The ...

  2. 一文带你理解URI 和 URL 有什么区别?

    当我们打开浏览器,要访问一个网站或者一个ftp服务器的时候,一定要输入一串字符串, 比如: https://blog.csdn.net/ 或者: ftp://192.168.0.111/ 这样我们就可 ...

  3. 使用 SpanMetrics Connector 将 OpenTelemetry 跟踪转换为指标

    原文:https://last9.io/blog/convert-opentelemetry-traces-to-metrics-using-spanconnector/ 如果您已经实施了跟踪但缺乏强 ...

  4. 【Git】之分支合并命令

    一.Git分支管理 1.查看Git分支 git branch 按Q键返回. 2.创建分支以及删除分支 git branch newtest git branch -d newtest 3.切换分支 g ...

  5. ES6之常用开发知识点:let 和 const 命令详解(二)

    let命令 基本用法 { let a = 10; var b = 1; } a // ReferenceError: a is not defined. b // 1 根据上面结果显示,let声明的变 ...

  6. 工具 – Cypress

    介绍 Cypress 是一款 e2e 测试工具.每当我们写好一个组件或者一个页面之后,我们会想对整体做一个测试. 在不使用工具的情况下,我们会开启 browser,然后做一系列点击.滚动.填 form ...

  7. ASP.NET Core Library – ImageSharp

    前言 2021 年就写过一篇了, Asp.net core 学习笔记 Image processing (ImageSharp), 只是那时还是旧的写法, 这篇作为翻新和以后继续增加新功能的介绍. I ...

  8. Bit, Byte, ASCII, Unicode, UTF, Base64

    前言 做项目偶尔会接触到 stream 这个感念,不管是 memory stream 还是 file stream,它们又会提到 bytes. 还有像 Identity – 安全基础知识 中提到的 S ...

  9. 前端VUE调用后台接口,实现基本增删改查

    设置接口请求 作为一个后台,个人一点感想:前端现在都是组件化开发,会看文档基本功能就能实现. js文件 import request from '@/router/axios' // 查询 expor ...

  10. mysql后台导入sql文件-设定字符集

    需求描述:有一个user_info.sql 的文件里面都是插入user_info表的insert语句数据,数据量500M,要求快速插入mysql的数据库中. 解决方法: 1.利用客户端工具加载文件插入 ...