Update 0583.两个字符串的删除操作.md about rust

This commit is contained in:
fwqaaq
2023-07-23 19:52:17 +08:00
committed by GitHub
parent eb632c6183
commit 0a83ee2c20

View File

@ -368,7 +368,31 @@ 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
}
}
```
<p align="center"> <p align="center">