当前位置:网站首页>最长连续序列
最长连续序列
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
都看到这里了,下面的内容你懂得,让我们共同进步!
边栏推荐
- [C language] how to generate normal or Gaussian random numbers
- open3d里pointcloud和numpy数组之间的转化
- sent2vec教程
- Seata数据库中出现以下问题要怎么解决呀?
- PMP真的有用吗?
- 坐拥1200亿,她又要IPO敲钟了
- Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan
- IonQ联合GE Research证实:量子计算在风险聚合上有巨大潜力
- 2022 welder (technician) examination question bank simulated examination platform operation
- Conversion between pointcloud and numpy arrays in open3d
猜你喜欢

Maingene listed on the Hong Kong Stock Exchange: IPO with a market value of HK $4.3 billion was ignored by the market

Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan

物联网低代码平台常用《组件介绍》

量子前沿英雄谱|“光量子探险家”McMahon:将任何物理系统变成神经网络

干货 | 科研人的KPI怎么算,H指数和G指数是什么

Kwai investment e-commerce service provider Yixin optimization

3. Caller 服务调用 - dapr

Vscode writes markdown file and generates pdf

PMP认证证书的续证费用是多少?

The hidden crisis of Weilai: past, present and future
随机推荐
【数字IC精品文章收录】近500篇文章|学习路线|基础知识|接口|总线|脚本语言|芯片求职|安全|EDA|工具|低功耗设计|Verilog|低功耗|STA|设计|验证|FPGA|架构|AMBA|书籍|
名创优品通过上市聆讯:寻求双重主要上市 年营收91亿
js 判断字符串为空或者不为空
QQ被盗号后群发黄图,大批用户“社死”
优巨新材冲刺深交所:拟募资6.5亿 年营收3.33亿
Le patron a donné trois ordres: discret, discret, discret
Leetcode(406)——根据身高重建队列
只出现一次的数字(水了个简单题)
What is the renewal fee for PMP certificate?
快手投资电商服务商易心优选
Work study management system based on ASP
Functools: high order functions and operations on callable objects (continuous updating ing...)
从莫高窟到太平洋,海量数据找到了新家园
加油站(贪心)
云杉网络DeepFlow帮助5G核心网和电信云构建可观测性
Ionq and Ge research confirmed that quantum computing has great potential in risk aggregation
量子前沿英雄谱|“光量子探险家”McMahon:将任何物理系统变成神经网络
2022 questions d'examen pour les cuisiniers chinois (Senior) et l'examen de simulation en ligne
Kwai investment e-commerce service provider Yixin optimization
Jackie Chan and fast brand, who is the Savior of Kwai?