leetcode 128. Longest Consecutive Sequence

深信服防火墙路由模式开局部署-手把手教学(小白篇) 手把手教学,拿到深信服的防火墙路由模式该如何开局? 阅读详情

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

Your algorithm should run in O(n) complexity.

Example:

Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.

思路:
如果能排序的话就比较简单,但是要求时间复杂度是O(n),而比较排序O(nlogn)
所以不用排序(虽然快速排序最佳是O(n))
注意最长连续子序列不包括重复元素,[4,4,5,5,6,6]的最长连续子序列长度是3, 而不是6

对每个元素,看它左边连续序列的长度和右边子序列长度
遍历每个元素,每个元素保存(key, value), 其中key是nums[i], value是在nums[i]处最长连续子序列的长度。
而nums[i]可能是以下几种情况
len代表当前元素nums[i]处最长连续子序列的长度

(1)nums[i]是最长连续子序列的右边界,如[1, 2]中的2
子序列的长度保存在左边界和右边界处,即key = nums[i] 和 nums[i - len_r] 处保存value = len, len_l指nums[i]左边的最长连续子序列长度,即看key=nums[i]-1处的value

(2)nums[i]是最长连续子序列的左边界,如[1, 2]中的1
子序列的长度保存在左边界和右边界处,即key = nums[i] 和 nums[i + len_r] 处保存value = len, len_r指nums[i]右边的最长连续子序列长度,即看key=nums[i]+1处的value

(3)nums[i]把左右子序列连接起来,如[1, 2] + 3 + [4, 5]
左边子序列的长度可以保存在2元素处,即看key=nums[i]-1处的value
右边子序列的长度可以保存在4元素处,即看key=nums[i]+1处的value
整体长度为len = len_r + len_r + 1
再更新nums[i], nums[i]-len_r, nums[i] + len_r,即更新元素本身(用来排除重复元素),左边界,右边界处的value到len
再更新整体最大值

由此可以看到需要一个HashMap保存元素和它对应的长度
对每个元素,仅需要根据它左右相邻的元素来得到左右子序列长度
每遍历一个元素,保存它自己和左右子序列边界处的元素对应的序列长度

    public int longestConsecutive(int[] nums) {
        HashMap<Integer, Integer> count = new HashMap<>();
        int maxLen = 0;
        
        for (int num : nums) {
            if (count.containsKey(num)) {
                continue;
            }
            
            int left = count.getOrDefault(num - 1, 0);
            int right = count.getOrDefault(num + 1, 0);
            int len = left + right + 1;
            
            maxLen = Math.max(maxLen, len);
            
            count.put(num, len);
            count.put(num - left, len);
            count.put(num + right, len);
        }
        
        return maxLen;
    }
matlab电偶极子源码,电偶极子的MATLAB场模拟 电偶极子周围电场电势分布的MATLAB三维模拟Matlab可以用于模拟四维场的效果,本文以电偶极子的电场和电势场为例展示MATLAB在该方面领域的应用。摘要电偶极子由一个正电荷和一个负电荷组成,其电场和空间电势分布与单电荷相比具有一些重要的特性。本文将给出电场和电势场的近似表达式。这两个量的精确表达式是复杂的,不太常用,因此 MATLAB 被采用来操作精确值和绘制三维分布的远区域, 近区域和一侧区... 阅读详情

相关推荐

上证指数30年k线图_金融占星简单解读上证指数30年历史行情

阅读此文需要一定的占星基础知识,本人对于金融知识了解甚少,如有描述错误请包涵指正!影响大众事件发展趋势的都是外行星,即木星-冥王星这5颗,由于时间跨度长达30年,绘制社会行星的K线图工作量太大,所以只以时代行星对这30年行情的简单解读作为分享,在今后的短期分析中会逐渐加入社会行星,甚至在个股分析上还会加入个人行星。那即使只绘制时代行星,所展现出来的信息也无法在一张图上画出来,否则太乱难以分辨,所以...

weixin_39831242的博客 1672

LeetCode 128: Longest Consecutive Sequence

128.Longest Consecutive Sequence Hard Given an unsorted array of integersnums, returnthe length of the longest consecutive elements sequence. Follow up:Could you implement theO(n)solution? Example 1: Input: nums = [100,4,200,1,3,2] Output: ...

roufoo的博客 230

实用电路精讲系列---快速精密桥式放大器电路

快速精密桥式放大器通过惠斯通电桥转换物理量为差分信号,经仪表放大器精密放大,具有高共模抑制、低噪声和宽带宽特性。其核心包含:电桥传感器转换、仪表放大器信号调理(增益可调、温漂补偿)、输出级适配ADC。典型应用包括智能断路器监测、胎压检测、电池安全预警等10大领域,需注意传感器-放大器-ADC最短路径布局。实用电路采用LT1057S和LT1010构建,实现稳定供电、差分处理、功率放大和桥式驱动,满足高精度、快速响应需求。该技术在工业控制、医疗设备等领域具有重要价值。

weixin_40590799的博客 1413

leetcode题解:第128题Longest Consecutive Sequence

题目 Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O(n) complexity. Example: Input: [100...

chenf1999的博客 444

scRNA-seq的fastq文件处理流程——持续更新

获得了一批新的scRNA-seq的fastq.gz文件,每个样本的数据形式为: 我打算使用10X cellranger处理fastq.gz文件,下载及安装方式在官网:https://support.10xgenomics.com/single-cell-gene-expression/software/downloads/2.0这个网址写了10X cellranger处理fastq.gz文件的命名规则https://support.10xgenomics.com/single-cell-gene-expr

watermel__的博客 3198

STM32L151C8T6低功耗实战:RTC闹钟唤醒停机模式的配置与优化

本文详细介绍了如何在STM32L151C8T6微控制器上配置与优化RTC闹钟唤醒停机模式,以实现极致的低功耗设计。文章从核心原理出发,结合STM32CubeMX配置实战,深入讲解了进入停机模式前的关键准备工作、唤醒后的系统恢复流程,并分享了将功耗降至个位数微安级的深度优化技巧与常见问题解决方案。

weixin_29166013的博客 298

LeetCode力扣之128. Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Re...

LeeWei 352

免费获取船舶轨迹数据?手把手教你用Python处理中国海洋卫星AIS数据

本文详细介绍了如何免费获取中国海洋卫星AIS数据,并使用Python进行船舶轨迹数据的处理与分析。从数据源评估到自动化处理流水线,再到高级分析与可视化技巧,手把手教你避开常见问题,实现高效数据处理。特别适合航运数据分析、物流优化和海上安全研究等领域的研究者和开发者。

weixin_42616115的博客 320

LeetCode 128. Longest Consecutive Sequence 题解

本题是哈希表的经典应用问题,主要考察对哈希表的理解和使用。通过使用哈希表,我们可以在 O(1) 的时间内判断一个元素是否存在,从而高效地找到最长连续序列。哈希表的核心思想是:将数组中的所有元素存储在哈希表中,然后遍历每个元素,当遇到一个连续序列的起点时,计算该连续序列的长度,并更新最长连续序列的长度。这种方法不仅适用于最长连续序列问题,还可以应用于许多其他需要快速查找元素的问题。掌握哈希表的使用,对于解决这类问题非常重要。

cannonjinx的博客 4544

【AI编程干货】Vibe Coding 技巧(适用于AI编程IDE)

用 AI 写代码这件事门槛低,谁都能对着对话框输入几句话让它生成代码,但生成的东西能不能用、出了问题能不能兜住,这中间的差距比想象中大得多。 我从去年开始把 AI 编码工具作为主力开发手段,中间经历过项目失控、代码雪崩、反复返工,慢慢摸索出一套相对稳定的工作方式。这篇把核心的五个习惯拆开聊聊,都是实际项目里验证过的。

blues_C的博客 621

python-pandapower电力系统潮流计算(算例8:ieee33节点潮流计算))

python-pandapower电力系统潮流计算(算例8:ieee33节点潮流计算))

总裁余(余登武)博客 5031

[LeetCode] 128. Longest Consecutive Sequence

[LeetCode] 128. Longest Consecutive Sequence 题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Your algorithm should run in O(n) complexity. 样例 Input...

weixin_30906701的博客 83

独创改进 | RT-DETR 引入 Asymptotic Hybrid Encoder | 渐进混合特征解码结构

独创改进🚀! | RT-DETR 引入 Asymptotic Hybrid Encoder | 渐进混合特征解码结构

Coding... 2211

【LeetCode】解题128:Longest Consecutive Sequence

LeetCode解题 128:Longest Consecutive SequenceProblem 128: Longest Consecutive Sequence [Hard]解题思路Solution (Java) Problem 128: Longest Consecutive Sequence [Hard] Given an unsorted array of integers, fin...

Fayedily的博客 279

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

leetcode128. Longest Consecutive Sequence

题目:题目链接 思路:最长连续序列,不会。下面介绍一种大神的解法和官方解答。 方法一 大神解法 利用map的有序性,把元素都丢到map里面,然后直接遍历一遍map就可以了。看代码: class Solution { public: int longestConsecutive(vector<int>& nums) { if (nums.size() < 2) return nums.size(); map<int, int>mp;

欲买桂花同载酒 3417

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

LeetCode128:Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O(n) complexity. Example: Input:[100, 4, 200, 1, 3, 2] Output: 4 Ex...

卷卷萌的博客 283

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Leetcode 128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3

无名山丘,崛起成峰 1133

LeetCode:128 Longest Consecutive Sequence

problem LeetCode 128. Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O(n) complexity. solution 思路过程 首先没有排序,还不知道最小的值是多少 最暴力的方法就是,找到最小值和最大值,然后就使维

erzhushashade的博客 170

前缀和 so easy! 力扣.128 最长连续序列 leetcode longest-consecutive-sequence

2)判断当前 nums[i] - nums[i-1]。给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。这种 one-pass 的需要理解清楚题目的意思,解决一些边界和特殊的场景问题。a. 等于 0,则两个数字相等。解释:最长数字连续序列是 [1, 2, 3, 4]。当然等于0的场景要看错误的测试用例才能知道,题目描述的并不够清晰。输入:nums = [0,3,7,2,5,8,4,6,0,1]输入:nums = [100,4,200,1,3,2]

438

LeetCode128—Longest Consecutive Sequence

LeetCode128—Longest Consecutive Sequence

上善若水 569

LeetCode Array 128 Longest Consecutive Sequence

128. Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O(n) complexity. Example: Input: [100,...

Yi Jiao's Blog 226

leetcode No128. Longest Consecutive Sequence

Question: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequenc

Dufre 705

Leetcode NO.128 Longest Consecutive Sequence

本题题目要求如下: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements seque

Programming my life 418

【LeetCode 128】 Longest Consecutive Sequence

题目描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O(n) complexity. Example: Input: [100, 4, 200, 1, 3, 2] Output: 4 Explanation: The longest consecutive elements sequence is

iCode_girl的博客 171
上一篇: 待解决:java generics collection initialization
下一篇: leetcode 153. Find Minimum in Rotated Sorted Array
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值