mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #2200 from fwqaaq/patch-52
Update 0583.两个字符串的删除操作.md about rust
This commit is contained in:
@ -370,7 +370,51 @@ function minDistance(word1: string, word2: string): number {
|
||||
};
|
||||
```
|
||||
|
||||
Rust:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn min_distance(word1: String, word2: String) -> i32 {
|
||||
let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1];
|
||||
for i in 0..word1.len() {
|
||||
dp[i + 1][0] = i + 1;
|
||||
}
|
||||
for j in 0..word2.len() {
|
||||
dp[0][j + 1] = j + 1;
|
||||
}
|
||||
for (i, char1) in word1.chars().enumerate() {
|
||||
for (j, char2) in word2.chars().enumerate() {
|
||||
if char1 == char2 {
|
||||
dp[i + 1][j + 1] = dp[i][j];
|
||||
continue;
|
||||
}
|
||||
dp[i + 1][j + 1] = dp[i][j + 1].min(dp[i + 1][j]) + 1;
|
||||
}
|
||||
}
|
||||
dp[word1.len()][word2.len()] as i32
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> 版本 2
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn min_distance(word1: String, word2: String) -> i32 {
|
||||
let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1];
|
||||
for (i, char1) in word1.chars().enumerate() {
|
||||
for (j, char2) in word2.chars().enumerate() {
|
||||
if char1 == char2 {
|
||||
dp[i + 1][j + 1] = dp[i][j] + 1;
|
||||
continue;
|
||||
}
|
||||
dp[i + 1][j + 1] = dp[i][j + 1].max(dp[i + 1][j]);
|
||||
}
|
||||
}
|
||||
(word1.len() + word2.len() - 2 * dp[word1.len()][word2.len()]) as i32
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
|
Reference in New Issue
Block a user