diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md index 11cf13d9..677c72c6 100644 --- a/problems/0300.最长上升子序列.md +++ b/problems/0300.最长上升子序列.md @@ -130,18 +130,16 @@ public: class Solution { public int lengthOfLIS(int[] nums) { int[] dp = new int[nums.length]; + int res = 0; Arrays.fill(dp, 1); - for (int i = 0; i < dp.length; i++) { + for (int i = 1; i < dp.length; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = Math.max(dp[i], dp[j] + 1); } + res = Math.max(res, dp[i]); } } - int res = 0; - for (int i = 0; i < dp.length; i++) { - res = Math.max(res, dp[i]); - } return res; } } @@ -294,7 +292,7 @@ function lengthOfLIS(nums: number[]): number { ```rust pub fn length_of_lis(nums: Vec) -> i32 { - let mut dp = vec![1; nums.len() + 1]; + let mut dp = vec![1; nums.len()]; let mut result = 1; for i in 1..nums.len() { for j in 0..i { @@ -309,7 +307,6 @@ pub fn length_of_lis(nums: Vec) -> i32 { ``` -