mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0300.最长上升子序列.md 优化 Rust 和 Java
This commit is contained in:
@ -130,18 +130,16 @@ Java:
|
|||||||
class Solution {
|
class Solution {
|
||||||
public int lengthOfLIS(int[] nums) {
|
public int lengthOfLIS(int[] nums) {
|
||||||
int[] dp = new int[nums.length];
|
int[] dp = new int[nums.length];
|
||||||
|
int res = 0;
|
||||||
Arrays.fill(dp, 1);
|
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++) {
|
for (int j = 0; j < i; j++) {
|
||||||
if (nums[i] > nums[j]) {
|
if (nums[i] > nums[j]) {
|
||||||
dp[i] = Math.max(dp[i], dp[j] + 1);
|
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;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -291,7 +289,7 @@ function lengthOfLIS(nums: number[]): number {
|
|||||||
Rust:
|
Rust:
|
||||||
```rust
|
```rust
|
||||||
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
|
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
|
||||||
let mut dp = vec![1; nums.len() + 1];
|
let mut dp = vec![1; nums.len()];
|
||||||
let mut result = 1;
|
let mut result = 1;
|
||||||
for i in 1..nums.len() {
|
for i in 1..nums.len() {
|
||||||
for j in 0..i {
|
for j in 0..i {
|
||||||
@ -306,13 +304,6 @@ pub fn length_of_lis(nums: Vec<i32>) -> i32 {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user