Skip to content

Commit 6b19a60

Browse files
Merge pull request youngyangyang04#2185 from fwqaaq/patch-45
Update 0300.最长上升子序列.md 优化 Rust 和 Java
2 parents 897a31c + 9a720d8 commit 6b19a60

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

problems/0300.最长上升子序列.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,16 @@ public:
130130
class Solution {
131131
public int lengthOfLIS(int[] nums) {
132132
int[] dp = new int[nums.length];
133+
int res = 0;
133134
Arrays.fill(dp, 1);
134-
for (int i = 0; i < dp.length; i++) {
135+
for (int i = 1; i < dp.length; i++) {
135136
for (int j = 0; j < i; j++) {
136137
if (nums[i] > nums[j]) {
137138
dp[i] = Math.max(dp[i], dp[j] + 1);
138139
}
140+
res = Math.max(res, dp[i]);
139141
}
140142
}
141-
int res = 0;
142-
for (int i = 0; i < dp.length; i++) {
143-
res = Math.max(res, dp[i]);
144-
}
145143
return res;
146144
}
147145
}
@@ -294,7 +292,7 @@ function lengthOfLIS(nums: number[]): number {
294292

295293
```rust
296294
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
297-
let mut dp = vec![1; nums.len() + 1];
295+
let mut dp = vec![1; nums.len()];
298296
let mut result = 1;
299297
for i in 1..nums.len() {
300298
for j in 0..i {
@@ -309,7 +307,6 @@ pub fn length_of_lis(nums: Vec<i32>) -> i32 {
309307
```
310308

311309

312-
313310
<p align="center">
314311
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
315312
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
 (0)