We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5fac31e commit a17de72Copy full SHA for a17de72
leetcode_Java/Solution0300.java
@@ -1,16 +1,23 @@
1
-// 300. 最长上升子序列
+// 300. 最长递增子序列
2
3
4
+/*
5
+1、定义dp数组:dp[i] 表示以索引i的元素结尾的最长递增子序列的长度
6
+2、状态转移方程:if (nums[i] > nums[j]) dp[i] = Math.max(dp[i], dp[j] + 1);
7
+ 位置i的最长递增子序列长度 等于j从0到i-1各个位置的 最长递增子序列加1的 最大值
8
+3、初始化:dp[i] = 1 表示每一个位置i的递增子序列的长度至少是1
9
+4、遍历dp数组填表:第一个for循环遍历dp数组的未知位置,第二个for循环遍历dp数组的已知位置,根据状态转移方程获取已知结果计算未知结果
10
+5、返回结果:遍历时比较每个位置结尾时的最长递增子序列的长度,并记录最大值,最后返回最大值
11
+ */
12
public class Solution {
13
public int lengthOfLIS(int[] nums) {
14
int n = nums.length;
- if (n < 2)
15
+ if (n < 2) {
16
return n;
-
17
+ }
18
int[] dp = new int[n];
19
Arrays.fill(dp, 1);
20
int res = 1;
21
for (int i = 1; i < n; i++) {
22
for (int j = 0; j < i; j++) {
23
if (nums[i] > nums[j]) {
0 commit comments