[leetcode] Longest Consecutive Sequence

s8654 The official Snapdragon Ride SX 4.0 development board provides standardized hardware support for the secondary development and functional verification of the s8654 platform. 阅读详情

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 in O(n) complexity.

这道题目要求了o(n)的时间复杂度,其实没有什么特别巧妙的做法,也就是对set容器的使用

int longestConsecutive(vector<int> &num) {
    unordered_map<int,int> s;
    int maxlen = 0;
    for(vector<int>::iterator it = num.begin(); it != num.end(); it++) s[*it] = 1;
    //each element in the array can be accessed at most twice,
    //therefore the amortized running time is still o(n).
    for(vector<int>::iterator it = num.begin(); it != num.end(); it++){
        if(s[*it] == 1){
            s[*it] = 0;
            int len = 1;
            int left = *it - 1;
            int right = *it + 1;

            while(s.find(left) != s.end() && s[left]){
                s[left--] = 0;
                len++;
            }
            while(s.find(right) != s.end() && s[right]){
                s[right++] = 0;
                len++;
            }
            if(maxlen < len) maxlen = len;
        }
    }
    return maxlen;
}


从0到1打造一台机器人走起来 1)本体机械结构设计,依据人体生物学和生物力学知识,结合机器人运动时各关节扭矩需求,进行合理选用电机,比如胯部需要大扭矩,腿部惯量小等2)硬件拓扑上,分层架构的信号层和功率协同工作,高带宽,低延时,硬实时3)步态规划设计与验证环节实机回溯和部署过程:Real2Sim,Sim2Sim,Sim2Real。 阅读详情

相关推荐

Verilog与综合:数字逻辑的代码化革命及其未来挑战

本文深入探讨Verilog硬件描述语言与逻辑综合技术在数字芯片设计中的革命性作用。Verilog实现了数字逻辑的代码化描述,而综合技术则将其自动转换为门级网表,大幅提升设计效率。文章重点分析了在先进工艺节点下面临的时序、功耗和物理效应等挑战,并展望了AI驱动综合优化的未来发展方向。

postgres8guard的博客 801

LeetCod_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. 二、题目大意 给定一个含有数字的列表,求连续数的最大长度。 三、思路分析 ...

路漫漫远修兮 249

从48V到800V:TI BMS芯片如何应对高压电池管理挑战?

电池管理系统是电动汽车和储能系统的核心,负责监控电池状态、保障安全并优化性能。其核心原理在于通过高精度测量电池电压、电流和温度,结合算法估算电池的荷电状态与健康状态,从而实现精准的能量管理与寿命预测。在技术价值层面,高精度测量能最大化利用电池化学窗口,提升能量密度与续航里程;而符合功能安全标准的设计则是防止热失控等安全事故的基石。随着汽车电气化从48V轻混系统向400V/800V高压平台演进,BMS面临着测量精度、功能安全、高压隔离通信和系统成本等多重挑战。德州仪器的BQ796xx和BQ75614系列芯片,

weixin_27017211的博客 342

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

Rapid SCADA MQTT

一、配置RapidSCADA 1、准备KpMqtt.dll、Jint.dll库文件,从网盘下载。 2、将以上两个文件拷贝到SCADA安装目录的Comm/KP目录下,如下: C:\SCADA\ScadaComm\KP 3、新建SCADA项目 4、新建Line 5、新建Device 6、拷贝Config目录下的KpMqtt_001.xml文件、KpMqtt_Job.js文件到项目Comm\Config下,如下示例链接 C:\SCADA\Projects\MQTTProject\Instances\Defau

mumanquan1的博客 3007

TSEND_C指令错误代码解析与实战排错指南

本文详细解析西门子PLC中TSEND_C指令的常见错误代码,包括连接建立、参数配置、资源限制和网络通信等错误类型。针对错误代码80C4等典型问题提供实战排错指南,帮助工程师快速诊断和解决工业通信中的连接故障,提升系统可靠性。

backprop5master的博客 411

《leetcode-php》找出数组中最长的连续串长度

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]...

less_cold的博客 438

基于Arduino 的 FM 调频收音机

这是一个基于 Arduino 的开源的收音机项目,它具备一台标准收音机的全部功能,从控制部分到外壳都可以自己 DIY。在项目中,我们将介绍让 FM 收音机调谐器的 RDA5807 模块与 I2C 协议进行通信的方法,整个过程只需要两根线就可以与 IC 通信,接线非常简单。3D 打印的外壳也非常简洁。总之,是个非常简单和实用的项目,来试试吧!

ByteBox的博客 1642

Leetcode全数组问题

目录 1、编号2 Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be

無名黑洞 9356

实战:如何用MIPI CSI-2高效传输RAW10图像数据(附带宽优化技巧)

本文深入探讨了MIPI CSI-2协议在RAW10图像数据传输中的高效应用,详细解析了4像素5字节打包机制及其带宽优化技巧。通过动态Lane分配、位深度自适应压缩和虚拟通道交错传输等实战方法,显著提升传输效率,适用于IoT摄像头开发等低功耗场景。

css33的专栏 732

麦克斯韦电磁场理论

1)从知识先后上说:麦克斯韦方程组为理解光的传播、反射、折射等现象提供了理论基础。不理解基础,怎么能理解上层建筑。2)从如何学懂上说,把基础学习的越牢,越深刻,后续学习上层建筑就会水到渠成一些,不会越学越多疑问。而是越学越少疑问。

weixin_55255438的博客 2825

Consecutive strings 练习源码

You are given an array strarr of strings and an integer k. Your task is to return the first longest string consisting of k consecutive strings taken in the array. Example: longest_consec([“zone”, “abi...

Pynithon的博客 381

Vue Print Designer——一款让打印模板的设计变简单的可视化打印设计器

Vue Print Designer——一款让打印模板的设计变简单的可视化打印设计器

bjzhang75的博客 684

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的博客 233

假人有限元模型

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

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

LeetCode 128. Longest Consecutive Sequence(哈希)

题目来源:https://leetcode.com/problems/longest-consecutive-sequence/ 问题描述 128.Longest Consecutive Sequence Hard Given an unsorted array of integers, find the length of the longest consecutive element...

da_kao_la的博客 136

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Leetcode之Longest Consecutive Sequence

[LeetCode] Longest Consecutive Sequence 求最长连续序列 Given an unsorted array of integers, findthe length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The lon

mween的博客 325

【Leetcode】Longest Consecutive Sequence

题目链接:https://leetcode.com/problems/longest-consecutive-sequence/ 题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100,

业精于勤,荒于嬉 1935

[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

Leetcode 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

每天都要有进步 296

leetcode之longest-consecutive-sequence(求数组最长连续数长度)

leetcode之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 lo...

此心光明,亦复何言~ 474

[leetcode]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

绝知此事要躬行 970

Leetcode之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...

漫漫学习路 384

Longest Consecutive Sequence Leetcode Python

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,

hyperbolechi的专栏 549

[LeetCode] Longest Consecutive Sequence

[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 longest consecutive elements sequence is [1,

1.02^365的成长裂变 3633

LeetCode 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

Coding for Dreams 1749
上一篇: [leetcode] Word Ladder II
下一篇: [leetcode] Sum Root to Leaf Numbers
和道一文字JC
博客等级 码龄19年 24粉丝 178原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值