当前位置:网站首页>最长连续序列
最长连续序列
2022-06-28 14:51:00 【华为云】
title: 最长连续序列
date: 2022-04-22 11:33:47
tags: 每天进步一点点系列
题目
题目:最长连续序列
难度:中等
给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。请你设计并实现时间复杂度为 O(n) 的算法解决此问题。
示例 1:
输入:nums = [100,4,200,1,3,2]
输出:4
解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。示例 2:
输入:nums = [0,3,7,2,5,8,4,6,0,1]
输出:9提示:0 <= nums.length <= 105
-109 <= nums[i] <= 109
题解:先排下序,用个一维dp数组,dp[n]记录长度,最后求出最大的dp值就行了,数组长度为0的先处理下就行了,注意点就是返回值默认值为1,先给dp数组都初始化为1,因为最小的连续长度肯定是包含自身也就是1
代码:
class Solution { public int longestConsecutive(int[] nums) { if(nums.length==0){ return 0; } //排序下 Arrays.sort(nums); //max初始化为1,因为最小的连续序列长度为1,避免数组中只有一个元素的情况,也可以在上面写if提前过滤 int max = 1; int[] dp = new int[nums.length]; //先填充个初始长度,包括本身,也就是1 Arrays.fill(dp,1); for(int i=1;i<nums.length;i++){ //比前一个大,那么就是连续的 if(nums[i]==nums[i-1]+1){ dp[i] = dp[i-1] + 1; } //和前一个相等,连续长度保持就行 if(nums[i]==nums[i-1]){ dp[i] = dp[i-1]; } max = Math.max(max,dp[i]); } return max; }}每日单词

以上就是最长连续序列(dp)全部内容
版权声明:
原创博主:牛哄哄的柯南
个人博客链接:https://www.keafmd.top/
看完如果对你有帮助,感谢点击下面的==一键三连==支持!
[哈哈][抱拳]

加油!
共同努力!
Keafmd
都看到这里了,下面的内容你懂得,让我们共同进步!
边栏推荐
- Numbers that only appear once
- 不要使用短路逻辑编写 stl sorter 多条件比较
- How to solve the following problems in the Seata database?
- Introduction to common components of IOT low code platform
- Conversion between pointcloud and numpy arrays in open3d
- seata-server 1.5.0 如何支持mysql8.0?
- Is PMP really useful?
- Softing epGate PB系列网关-可将PROFIBUS总线集成到EtherNet/IP网络
- [C language] implementation of binary tree and three Traversals
- Angers medical sprint scientific innovation board: annual revenue of RMB 300million and proposed fund raising of RMB 770million
猜你喜欢

2022年最新PyCharm激活破解码永久_详细安装教程(适用多版本)

名创优品通过上市聆讯:寻求双重主要上市 年营收91亿

币圈大地震:去年赚100万,今年亏500万

WPF 视频硬解码渲染播放(无空域)(支持4K、8K、高帧率视频)

叮!Techo Day 腾讯技术开放日如约而至!

Classmate Zhang hasn't learned to be an anchor yet

Dry goods | how to calculate the KPI of scientific researchers, and what are the h index and G index

Q-Tester 3.2:适用于开发、生产和售后的诊断测试软件
技术弄潮儿

干货 | 科研人的KPI怎么算,H指数和G指数是什么
随机推荐
[C language] how to generate normal or Gaussian random numbers
5000倍回报,南非报业投资腾讯赚了一个省
Flutter series: offstage in flutter
Classmate Zhang hasn't learned to be an anchor yet
Conversion between pointcloud and numpy arrays in open3d
Kwai investment e-commerce service provider Yixin optimization
腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元
Power battery is divided up like this
请问一下,是不是insert all这种oracle的批量新增没拦截?
证券公司和银行哪个更安全 怎么办理开户最安全
名创优品通过上市聆讯:寻求双重主要上市 年营收91亿
flutter 系列之:flutter 中的 offstage
开闭原则
2022 recurrent training question bank and online simulation examination for safety inspection of metal and nonmetal mines (underground mines)
What are the benefits of this PMP certificate?
Rails advanced -- framework theory cognition and construction scheme construction (I)
Maingene listed on the Hong Kong Stock Exchange: IPO with a market value of HK $4.3 billion was ignored by the market
Four visualization tools are recommended to solve 99% of large screen visualization projects!
2022 Chinese cook (Advanced) test questions and online simulation test
使用LamdbaUpdateWrapper的setSql作用及风险