mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加1035.不相交的线和1143.最长公共子序列
This commit is contained in:
@ -111,7 +111,6 @@ class Solution:
|
|||||||
Golang:
|
Golang:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
||||||
func maxUncrossedLines(A []int, B []int) int {
|
func maxUncrossedLines(A []int, B []int) int {
|
||||||
m, n := len(A), len(B)
|
m, n := len(A), len(B)
|
||||||
dp := make([][]int, m+1)
|
dp := make([][]int, m+1)
|
||||||
@ -140,7 +139,26 @@ func max(a, b int) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Rust:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
pub fn max_uncrossed_lines(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
|
||||||
|
let (n, m) = (nums1.len(), nums2.len());
|
||||||
|
let mut last = vec![0; m + 1]; // 记录滚动数组
|
||||||
|
let mut dp = vec![0; m + 1];
|
||||||
|
for i in 1..=n {
|
||||||
|
dp.swap_with_slice(&mut last);
|
||||||
|
for j in 1..=m {
|
||||||
|
if nums1[i - 1] == nums2[j - 1] {
|
||||||
|
dp[j] = last[j - 1] + 1;
|
||||||
|
} else {
|
||||||
|
dp[j] = last[j].max(dp[j - 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dp[m]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
|
||||||
|
@ -193,6 +193,28 @@ func max(a,b int)int {
|
|||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Rust:
|
||||||
|
```rust
|
||||||
|
pub fn longest_common_subsequence(text1: String, text2: String) -> i32 {
|
||||||
|
let (n, m) = (text1.len(), text2.len());
|
||||||
|
let (s1, s2) = (text1.as_bytes(), text2.as_bytes());
|
||||||
|
let mut dp = vec![0; m + 1];
|
||||||
|
let mut last = vec![0; m + 1];
|
||||||
|
for i in 1..=n {
|
||||||
|
dp.swap_with_slice(&mut last);
|
||||||
|
for j in 1..=m {
|
||||||
|
dp[j] = if s1[i - 1] == s2[j - 1] {
|
||||||
|
last[j - 1] + 1
|
||||||
|
} else {
|
||||||
|
last[j].max(dp[j - 1])
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dp[m]
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Javascript:
|
Javascript:
|
||||||
|
Reference in New Issue
Block a user