当前位置:网站首页>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;
}
}边栏推荐
猜你喜欢
随机推荐
The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
【DesignMode】享元模式(Flyweight Pattern)
DNS 系列(一):为什么更新了 DNS 记录不生效?
Talk about the realization of authority control and transaction record function of SAP system
LeetCode 403. 青蛙过河 每日一题
A tour of gRPC:03 - proto序列化/反序列化
Cesium (4): the reason why gltf model is very dark after loading
Horizontal and vertical centering method and compatibility
Pycharm terminal enables virtual environment
最新高频Android面试题目分享,带你一起探究Android事件分发机制
Prediction - Grey Prediction
值得一看,面试考点与面试技巧
Introduction to ThinkPHP URL routing
字节跳动Android面试,知识点总结+面试题解析
Opportunity interview experience summary
Detailed explanation of several ideas for implementing timed tasks in PHP
[designmode] flyweight pattern
Deep listening array deep listening watch
Advanced C language -- function pointer
logback. XML configure logs of different levels and set color output


![[vulnhub range] thales:1](/img/fb/721d08697afe9b26c94fede628c4d1.png)




![[Android -- data storage] use SQLite to store data](/img/f6/a4930276b3da25aad3ab1ae6f1cf49.png)