Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60bb5dc

Browse files
authored
Update 0072.编辑距离.md
1 parent a7e3595 commit 60bb5dc

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎problems/0072.编辑距离.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,36 @@ impl Solution {
429429
}
430430
```
431431

432+
> 一维 dp
433+
434+
```rust
435+
impl Solution {
436+
pub fn min_distance(word1: String, word2: String) -> i32 {
437+
let mut dp = vec![0; word1.len() + 1];
438+
for (i, v) in dp.iter_mut().enumerate().skip(1) {
439+
*v = i;
440+
}
441+
442+
for char2 in word2.chars() {
443+
// 相当于 dp[i][0] 的初始化
444+
let mut pre = dp[0];
445+
dp[0] += 1; // j = 0, 将前 i 个字符变成空串的个数
446+
for (j, char1) in word1.chars().enumerate() {
447+
let temp = dp[j + 1];
448+
if char1 == char2 {
449+
dp[j + 1] = pre;
450+
} else {
451+
dp[j + 1] = dp[j + 1].min(dp[j]).min(pre) + 1;
452+
}
453+
pre = temp;
454+
}
455+
}
456+
457+
dp[word1.len()] as i32
458+
}
459+
}
460+
```
461+
432462
<p align="center">
433463
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
434464
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /