mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 12:15:58 +08:00
Update 0072.编辑距离.md about rust
This commit is contained in:
@ -401,6 +401,34 @@ int minDistance(char * word1, char * word2){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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 1..=word2.len() {
|
||||||
|
dp[0][i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (j, v) in dp.iter_mut().enumerate().skip(1) {
|
||||||
|
v[0] = j;
|
||||||
|
}
|
||||||
|
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]).min(dp[i][j]) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dp[word1.len()][word2.len()] as 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