最长连续序列

题目链接: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. CRC算法原理、推导及实现

    CRC, Cyclic Redundancy Check, 循环冗余校验 1. 基本原理 CRC的本质是除法,把待检验的数据当作一个很大(很长)的被除数,两边选定一个除数(有的文献叫poly),最后得 ...

  2. Nano 编辑器入门

    按键映射 ⌃: Control M: Meta (Alt) 编辑文件: nano MyFile.txt 退出并保存: ⌃-X 退出(此时 nano 会提示你要不要保存,按 Y 保存) Nano 提示输 ...

  3. 折腾 Quickwit,Rust 编写的分布式搜索引擎 - 可观测性之分布式追踪

    概述 分布式追踪是一种跟踪应用程序请求流经不同服务(如前端.后端.数据库等)的过程.它是一个强大的工具,可以帮助您了解应用程序的工作原理并调试性能问题. Quickwit 是一个用于索引和搜索非结构化 ...

  4. 关于人工智能的思考,写在chatGPT爆火之时

    今天是2023年3月22日,今天思维比较活跃,故作文一篇,以记录当下所想. 先是回家询问了未婚妻的想法,然后记录自己的想法. 未婚妻的想法: 1.在AI领域已经滞后于世界了.因为在墙头上看到过一个加拿 ...

  5. Vue3 动态子页面和菜单栏同步

    动态子页面 <router-view></router-view>显示子页面的内容 main.vue <template> <a-layout id=&quo ...

  6. nuxt(搁置)

    https://nuxt.com.cn/docs/getting-started/installation 开始使用 全栈Web应用和网站 Nuxt使用约定和一套规范的目录结构来自动化重复的任务,让开 ...

  7. 系统编程-进程-exec系列函数超级详解(带各种实操代码)

    我的相关博文: 系统编程-进程-close-on-exec机制 PART1  exec系列函数功能简介 exec系列函数登场 常规操作是先fork一个子进程,然后在子进程中调用exec系列函数执行新的 ...

  8. 系统编程-进程-vfork使用、浅析

    1. 先贴代码 #include <stdio.h> #include <stdlib.h> #include <unistd.h> int globvar = 6 ...

  9. Java中浮点数运算存在的精度问题以及解决方法

    观察以下一段代码,相信小朋友都可以一眼看出答案,但是计算机给出的答案是这样吗? public class TestDouble { public static void main(String arg ...

  10. 批量读取dicom数据 to array类型((多标签融合)))

    file_name = ["portalvein", "venoussystem", "venacava"] def read_dicom( ...