当前位置:网站首页>Longest continuous sequence
Longest continuous sequence
2022-06-28 15:07:00 【Hua Weiyun】
title: The longest continuous sequence
date: 2022-04-22 11:33:47
tags: Make a little progress every day
subject
subject : The longest continuous sequence
difficulty : secondary
Given an array of unsorted integers nums , Find the longest sequence of consecutive numbers ( Sequence elements are not required to be contiguous in the original array ) The length of . Please design and implement it with a time complexity of O(n) The algorithm to solve this problem .
Example 1:
Input :nums = [100,4,200,1,3,2]
Output :4
explain : The longest consecutive sequence of numbers is [1, 2, 3, 4]. Its length is 4.Example 2:
Input :nums = [0,3,7,2,5,8,4,6,0,1]
Output :9Tips :0 <= nums.length <= 105
-109 <= nums[i] <= 109
Answer key : First in the next order , One dimension dp Array ,dp[n] Record length , Finally, find the maximum dp It's worth it , The array length is 0 Just deal with it first , Note that the default value of the return value is 1, First give dp Arrays are initialized to 1, Because the smallest continuous length must contain itself, that is 1
Code :
class Solution { public int longestConsecutive(int[] nums) { if(nums.length==0){ return 0; } // Sort below Arrays.sort(nums); //max Initialize to 1, Because the minimum continuous sequence length is 1, Avoid the case where there is only one element in the array , You can also write on it if Filtering ahead of time int max = 1; int[] dp = new int[nums.length]; // First fill in an initial length , Including itself , That is to say 1 Arrays.fill(dp,1); for(int i=1;i<nums.length;i++){ // Bigger than the previous one , So it's continuous if(nums[i]==nums[i-1]+1){ dp[i] = dp[i-1] + 1; } // Equal to the previous one , Just keep the continuous length if(nums[i]==nums[i-1]){ dp[i] = dp[i-1]; } max = Math.max(max,dp[i]); } return max; }}Daily words

The above is the longest continuous sequence (dp) The whole thing
Copyright notice :
Original Blogger : Cowherd Conan
Personal blog links :https://www.keafmd.top/
If it helps you , Thank you for clicking on == One key, three links == Support !
[ ha-ha ][ Huai Quan ]

come on. !
Joint efforts !
Keafmd
You can see it here , You know the following , Let's make progress together !
边栏推荐
- R语言ggplot2可视化:使用patchwork包将两个ggplot2可视化结果纵向堆叠起来(stacking)形成组合图、一个可视化结果堆叠在另外一个可视化结果上
- 【算法篇】刷了两道大厂面试题,含泪 ”重学数组“
- Tencent was underweight again by prosus, the major shareholder: the latter also cashed out $3.7 billion from JD
- 从五大能力到 “1+5+N”,华为让政企转型更稳健
- 鸟类飞行状态下穿戴式神经信号与行为数据检测记录系统的技术难点总结
- 成龙和快品牌,谁才是快手的救星?
- 【mysql学习笔记24】索引设计原则
- Combined sum leetcode
- 考了这个PMP证书到底有什么好处?
- How does Seata server 1.5.0 support mysql8.0?
猜你喜欢

Mingchuangyou products passed the listing hearing: seeking dual main listing with an annual revenue of 9.1 billion

老板嘱咐了三遍:低调、低调、低调
技术弄潮儿

Performance comparison of deep learning models on cat and dog image data sets

论文解读(GCC)《Efficient Graph Convolution for Joint Node RepresentationLearning and Clustering》

【算法篇】刷了两道大厂面试题,含泪 ”重学数组“

SAP MTS/ATO/MTO/ETO专题之九:M+M模式前后台操作,策略用50,提前准备原材料和半成品

Kwai investment e-commerce service provider Yixin optimization

Construction and management practice of ByteDance buried point data flow

Jackie Chan and fast brand, who is the Savior of Kwai?
随机推荐
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和数据表格横向组合起来形成最终结果图
叮!Techo Day 腾讯技术开放日如约而至!
云杉网络DeepFlow帮助5G核心网和电信云构建可观测性
ROS知识点——话题消息的定义与使用
BatchNorm2d原理、作用及其pytorch中BatchNorm2d函数的参数讲解
实验6 8255并行接口实验【微机原理】【实验】
C#/VB. Net to convert PDF to excel
MIPS汇编语言学习-03-循环
智慧园区数智化供应链管理平台如何优化流程管理,驱动园区发展提速增质?
【mysql学习笔记24】索引设计原则
PostgreSQL实现按年、月、日、周、时、分、秒的分组统计
WSUS客户端访问服务端异常报错-0x8024401f「建议收藏」
After QQ was stolen, a large number of users "died"
Q-Tester 3.2:适用于开发、生产和售后的诊断测试软件
【mysql学习笔记23】索引优化
Tencent was underweight again by prosus, the major shareholder: the latter also cashed out $3.7 billion from JD
Express模板引擎
老板囑咐了三遍:低調、低調、低調
Could you tell me whether the batch addition of Oracle such as insert all was not blocked?
论文解读(GCC)《Efficient Graph Convolution for Joint Node RepresentationLearning and Clustering》