LeetCode 128: Longest Consecutive Sequence

Excel-MCP实战指南:零代码实现大模型与Excel的智能交互 本文详细介绍了Excel-MCP工具如何实现大模型与Excel的无缝交互,无需编写代码即可完成数据读写、图表生成等复杂操作。通过实战案例展示了Excel-MCP在销售报表自动化、财务对账等企业级场景中的应用,大幅提升工作效率。文章还提供了安装指南、核心功能解析和常见问题解决方案。 阅读详情

128. Longest Consecutive Sequence

Hard

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

Follow up: Could you implement the O(n) solution? 

 

Example 1:

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

Example 2:

Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9

 

Constraints:

  • 0 <= nums.length <= 104
  • -109 <= nums[i] <= 109

解法1:unordered_set
参考的花生酱的网页
https://zxi.mytechroad.com/blog/hashtable/leetcode-128-longest-consecutive-sequence/
这里最关键的是           if (us.find(nums[i] - 1) == us.end()) 
这样当输入是[100,4,200,1,3,2],我们碰到4,3,2就不会再进if了,这样保证时间复杂度O(n)。

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set us(nums.begin(), nums.end());
        int n = nums.size();
        int res = 0;
        for (int i = 0; i < n; ++i) {
            if (us.find(nums[i] - 1) == us.end()) {
                int count = 0;
                int candidate = nums[i];
                while(us.find(candidate) != us.end()) {
                    count++;
                    candidate++;
                }
                res = max(res, count);
            }
        }

        return res;
    }
};

解法2:TBD

解法3:我有个想法是用线性建堆法把nums[]建成一个最小堆,然后逐个pop,统计其中最大连续的个数。不过这样也是O(nlogn)。

OpenCV实战之人脸美颜美型(八)——大眼(上) 从大众审美的角度来看,眼睛适当的“变大”会让人显得更加年轻。因此美型中就有一项大眼功能,其效果就是让眼睛适当变大,这章内容就围绕着大眼算法展开。 阅读详情

相关推荐

STM32 CubeMX实战:手把手教你用I2C驱动IST8310磁力计(附完整代码)

本文详细介绍了如何使用STM32 CubeMX和I2C驱动IST8310磁力计,从硬件连接到代码实现的完整流程。重点讲解了I2C通信异常处理、传感器初始化序列优化以及中断驱动数据采集方案,帮助开发者快速搭建可靠的地磁场数据采集系统。

weixin_42531163的博客 189

Leetcode 128. 最长连续序列 Longest Consecutive Sequence

中文描述: 给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。 题目描述: Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. Example 1: Input: nums = [100,4,200,1,3,2] Output: 4 Explanation: The longest cons

weixin_43946031的博客 304

从SPI到JTAG:玩转FT2232H的MPSSE多协议切换指南(附配置代码)

本文详细介绍了如何利用FT2232H芯片的MPSSE技术实现SPI与JTAG协议的无缝切换,提供配置代码和调试技巧。通过解析MPSSE引擎架构和硬件特性,帮助开发者灵活应对多种串行协议需求,提升嵌入式开发效率。特别适合FPGA调试和物联网设备开发场景。

sun99的博客 254

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

亚博k210视觉模块与stm32双向通信

【代码】亚博k210视觉模块与stm32双向通信。

w_150831的博客 3244

Calibre LVS效率翻倍秘籍:用Hierarchical Flow处理亿级晶体管设计的5个关键阶段

本文详细解析了如何利用Hierarchical Flow技术提升Calibre LVS在亿级晶体管SoC设计中的验证效率。通过五个关键阶段的分层验证流程,包括顶层抽象验证、模块级独立验证等,可显著降低内存占用40-60%,缩短迭代周期50-70%,特别适用于SMIC等先进工艺节点。文章还分享了算力资源优化、并行化处理等实战技巧,帮助工程师应对大规模芯片验证挑战。

sam99的博客 768

LeetCode-128. Longest Consecutive Sequence [C++][Java]

Given an unsorted array of integersnums, returnthe length of the longest consecutive elements sequence. You must write an algorithm that runs inO(n)time.

贫道绝缘子的博客 1312

S32K144 ADC硬件触发实战:PDB与TRGMUX配置避坑指南

本文深入解析了S32K144微控制器中ADC硬件触发的两种核心方案:PDB与TRGMUX。通过对比其架构差异、详细配置流程及常见问题排查,提供了从寄存器操作到DMA联动的完整优化指南,旨在帮助开发者解决多通道采样时序不稳、CPU负载过高等实际难题,实现高效稳定的数据采集。

weixin_29168153的博客 440

leetcode 128 最长连续序列 (Longest Consecutive Sequence)

给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。 进阶:你可以设计并实现时间复杂度为 O(n) 的解决方案吗? Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. Follow up: Could you implement the O(n) solution? Example 1: Input:

CLZHIT的博客 388

告别商业软件?实测用gnina(CNN打分版)做分子对接:从安装到跑通第一个案例

本文详细介绍了基于卷积神经网络(CNN)的开源分子对接工具gnina的安装与实战应用。从环境配置、编译安装到运行第一个COVID-19主蛋白酶抑制剂对接案例,全面解析了这款替代商业软件的强大功能,特别适合预算有限的研究团队进行药物发现和虚拟筛选。

weixin_42522320的博客 255

基于STM32、HAL库的ATMXT1066TD触摸屏控制器驱动程序设计

0x11) // 0x11是ATMXT1066的家族ID。// 最多支持10点触摸。// 足够存储10点触摸数据。

corlin6688的博客 164

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

手把手教你完成YOLOv10 PySide6目标检测界面搭建,使用Qt6设计YOLOv10检测系统,前台系统+后台管理系统开发实战,可用于大论文凑工作量或毕设必备,全网最详细教程

手把手教你完成YOLOv10 PySide6目标检测界面搭建,使用Qt6设计YOLOv10检测系统,前台系统+后台管理系统开发实战,可用于大论文凑工作量或毕设必备,全网最详细教程

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 1732

【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

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

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

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Leetcode 128:Longest Consecutive Sequence

TBA

想做程序媛的小太阳 175

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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

le119126的专栏 482

LeetCode-128:Longest Consecutive Sequence

题目 给出一个未排序的数组,找出最长的连续序列,连续指的是像……11、12、13、14……这样的,要求算法时间复杂度为O(n)。 做一些说明:不知道会不会有重复的数字,如果有应该怎么算?看最后AC的情况,应该是如果有重复的数字,不算在序列里边。 思路 1、自己的思路 我刚开始是想用hash结构来处理,不过又觉得hash好像时间复杂度也挺高,不知道谁给我的hash时间复杂度搞得感觉,是上...

DragonFreedom的博客 339

leetcode 128: Longest Consecutive Sequence

Use the unordered set which has the O(1) time complexity when accessing one number. The key here is to update the max only when I find one number is the left most number in one range. class Solution

mikefan1991的博客 205

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. For example, Given [100, 4, 200, 1, 3, 2], The ...

杨领well的专栏 529

Leetcode: 128. Longest Consecutive Sequence

Leetcode: 128. Longest Consecutive Sequence求对一个无序序列,求其中排序后最长连续序列,并给定了O(n)复杂度限制。解法既然要O(n)算法,排序显然不行,所以自然想到用hash table。将序列中的所有数存到一个unordered_set中。对于序列里任意一个数A[i],我们可以通过set马上能知道A[i]+1和A[i]-1是否也在序列中。如果在,继续找A

qq_16318319的博客 283

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.For example, Given [100, 4, 200, 1, 3, 2], The longest con

u012563267的博客 228

LeetCode:128. Longest Consecutive Sequence

在看这篇文章前,你也许想要先看看并查集是如何实现的:https://blog.youkuaiyun.com/weixin_43462819/article/details/83626022 这一题是实现完并查集之后练手的第二题,可以先看看第一题: https://blog.youkuaiyun.com/weixin_43462819/article/details/83628052 题目是这样的: Given an u...

weixin_43462819的博客 185

LeetCode: Longest Consecutive Sequence [128]

【题目】 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]. Return its length: 4. Your algorithm should run

Harry Huang 843

LeetCode:128. Longest Consecutive Sequence - Python

128. 最长连续序列 问题描述: 给定一个未排序的整数数组,找出最长连续序列的长度。 要求算法的时间复杂度为 O(n)。 示例 : 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1, 2, 3, 4]。它的长度为 4。 问题分析: (1) (2) Python3实现: # 官方解答 class Solution: def longestC...

Growth Diary 1210

LeetCode 128: Longest Consecutive Sequence

Difficulty: 4 Frequency: 3 Problem: 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

claviclelau的专栏 349
上一篇: 三分查找小结
下一篇: LeetCode 547: Friend Circles (并查集好题)
roufoo
博客等级 码龄15年 526粉丝 958原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值