当前位置:网站首页>LeetCode 300. 最长递增子序列 每日一题
LeetCode 300. 最长递增子序列 每日一题
2022-07-07 15:32:00 【@小红花】
问题描述
给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。
子序列 是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。
示例 1:输入:nums = [10,9,2,5,3,7,101,18]
输出:4
解释:最长递增子序列是 [2,3,7,101],因此长度为 4 。
示例 2:输入:nums = [0,1,0,3,2,3]
输出:4
示例 3:输入:nums = [7,7,7,7,7,7,7]
输出:1
提示:
1 <= nums.length <= 2500
-104 <= nums[i] <= 104来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/longest-increasing-subsequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Java
class Solution {
public int lengthOfLIS(int[] nums) {
int n = nums.length;
if(n == 1) return 1;
int[] dp = new int[n];
dp[0] = 1;
int ans = 0;
for(int i = 1;i < n;i++){
dp[i] = 1;
for(int j = 0;j < i;j++){
if(nums[j] < nums[i]){
dp[i] = Math.max(dp[i],dp[j] + 1);
}
}
ans = Math.max(ans,dp[i]);
}
return ans;
}
}
边栏推荐
猜你喜欢
爬虫(17) - 面试(2) | 爬虫面试题库
time标准库
字节跳动Android金三银四解析,android面试题app
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
最新2022年Android大厂面试经验,安卓View+Handler+Binder
Opencv personal notes
1亿单身男女“在线相亲”,撑起130亿IPO
Vs2019 configuration matrix library eigen
值得一看,面试考点与面试技巧
Lowcode: four ways to help transportation companies enhance supply chain management
随机推荐
Prediction - Grey Prediction
LeetCode 1155. 掷骰子的N种方法 每日一题
二叉搜索树(基操篇)
logback.xml配置不同级别日志,设置彩色输出
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
Find tags in prefab in unity editing mode
面向接口编程
Laravel changed the session from file saving to database saving
Module VI
数据中台落地实施之法
预测——灰色预测
The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
Three. JS series (3): porting shaders in shadertoy
Opencv personal notes
直接上干货,100%好评
华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
Personal notes of graphics (2)
JS modularization
【图像传感器】相关双采样CDS
[PHP] PHP interface inheritance and interface multi inheritance principle and implementation method