码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • leetcode 128. Longest Consecutive Sequence 最长连续序列(中等)


    一、题目大意

    https://leetcode.cn/problems/longest-consecutive-sequence

    给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。

    请你设计并实现时间复杂度为 O(n) 的算法解决此问题。

    示例 1:

    输入:nums = [100,4,200,1,3,2]
    输出:4
    解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
    示例 2:

    输入:nums = [0,3,7,2,5,8,4,6,0,1]
    输出:9

    提示:

    0 <= nums.length <= 105
    -109 <= nums[i] <= 109

    二、解题思路

    可以把所有数字放到一个哈希表,然后不断地从哈希表中任意取一个值,并删除掉其之前之后的所有连续数字,然后更新目前的最长连续序列长度。重复这一过程,就可以找到所有的连续数字序列,顺便找出最长的。

    三、解题方法

    3.1 Java实现-超时版

    public class Solution1 {
        public int longestConsecutive(int[] nums) {
            Set<Integer> intSet = new HashSet<>();
            for (int num : nums) {
                intSet.add(num);
            }
            int ans = 0;
            while (!intSet.isEmpty()) {
                int cur = intSet.stream().findFirst().get();
                intSet.remove(cur);
                int pre = cur - 1;
                int next = cur + 1;
                while(intSet.contains(pre)) {
                    intSet.remove(pre--);
                }
                while(intSet.contains(next)) {
                    intSet.remove(next++);
                }
                ans = Math.max(ans, next - pre - 1);
            }
            return ans;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    3.2 Java实现-通过版

    public class Solution {
        public int longestConsecutive(int[] nums) {
            Set<Integer> intSet = new HashSet<>();
            for (int num : nums) {
                intSet.add(num);
            }
            int ans = 0;
            for (int num : nums) {
                if (intSet.remove(num)) {
                    int pre = num - 1;
                    int next = num + 1;
                    while (intSet.remove(pre)) {
                        pre--;
                    }
                    while (intSet.remove(next)) {
                        next++;
                    }
                    ans = Math.max(ans, next - pre - 1);
                }
            }
            return ans;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    四、总结小记

    • 2022/8/16 Map的好些方法在处理大数据量时要慎用呀
  • 相关阅读:
    你需要的免费热门API接口这里都有~
    Linux搭建我的世界服务器 + 公网远程联机教程「不需要公网IP」
    debug过程中,矩阵左乘右乘相关概念梳理
    Java IO流(上)
    Springboot+vue的医患档案管理系统。Javaee项目,springboot vue前后端分离项目。
    DVWA靶机,通过XSS盗取cookie登录
    二叉树的经典OJ题
    机器学习 --- PCA
    SpringCloud学习笔记(十四)Sentinel 实现熔断与限流
    进程与线程
  • 原文地址:https://blog.csdn.net/ln_ydc/article/details/126375208
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号