mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Update 0583.两个字符串的删除操作.md
This commit is contained in:
@ -394,6 +394,26 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
> 版本 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">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user