当前位置:网站首页>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 !
边栏推荐
- 请问一下,是不是insert all这种oracle的批量新增没拦截?
- R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用回归系数及其标准误计算每个系数对应的Z统计量的值
- High "green premium" of environmental protection products? How far is the low-carbon lifestyle from people
- Le patron a donné trois ordres: discret, discret, discret
- R语言ggplot2可视化:patchwork包将一个ggplot2可视化结果和一个plot函数可视化结果横向组合起来形成最终结果图、两个可视化的组合结果对齐、并为组合图像的每个子图添加标题
- After QQ was stolen, a large number of users "died"
- [C language] implementation of binary tree and three Traversals
- What is the renewal fee for PMP certificate?
- The boss told me three times: low key, low key, low key
- 实验6 8255并行接口实验【微机原理】【实验】
猜你喜欢
随机推荐
币圈大地震:去年赚100万,今年亏500万
ROS知识点——ROS创建工作空间
考了这个PMP证书到底有什么好处?
股票开户优惠链接,我如何才能得到?手机开户是安全么?
新零售线下店逆势起飞,通膨乌云下的消费热情
Case driven: a detailed guide from getting started to mastering shell programming
ROS知识点——话题消息的定义与使用
What is the renewal fee for PMP certificate?
厨卫电器行业S2B2C系统网站解决方案:打造S2B2C平台全渠道商业系统
验证回文串
GBASE南大通用亮相第六届世界智能大会
Leetcode (665) -- non decreasing column
Does Frankfurt currently support SQL?
使用LamdbaUpdateWrapper的setSql作用及风险
Unable to create process using 'd:\program file
Send2vec tutorial
Vector explanation + topic
技术弄潮儿
Functools: high order functions and operations on callable objects (continuous updating ing...)
Facebook出手!自适应梯度打败人工调参









