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 65bf55b

Browse files
Merge pull request youngyangyang04#2200 from fwqaaq/patch-52
Update 0583.两个字符串的删除操作.md about rust
2 parents af97fc1 + d6bf0c5 commit 65bf55b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

‎problems/0583.两个字符串的删除操作.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,51 @@ function minDistance(word1: string, word2: string): number {
370370
};
371371
```
372372

373+
Rust:
374+
375+
```rust
376+
impl Solution {
377+
pub fn min_distance(word1: String, word2: String) -> i32 {
378+
let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1];
379+
for i in 0..word1.len() {
380+
dp[i + 1][0] = i + 1;
381+
}
382+
for j in 0..word2.len() {
383+
dp[0][j + 1] = j + 1;
384+
}
385+
for (i, char1) in word1.chars().enumerate() {
386+
for (j, char2) in word2.chars().enumerate() {
387+
if char1 == char2 {
388+
dp[i + 1][j + 1] = dp[i][j];
389+
continue;
390+
}
391+
dp[i + 1][j + 1] = dp[i][j + 1].min(dp[i + 1][j]) + 1;
392+
}
393+
}
394+
dp[word1.len()][word2.len()] as i32
395+
}
396+
}
397+
```
373398

399+
> 版本 2
400+
401+
```rust
402+
impl Solution {
403+
pub fn min_distance(word1: String, word2: String) -> i32 {
404+
let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1];
405+
for (i, char1) in word1.chars().enumerate() {
406+
for (j, char2) in word2.chars().enumerate() {
407+
if char1 == char2 {
408+
dp[i + 1][j + 1] = dp[i][j] + 1;
409+
continue;
410+
}
411+
dp[i + 1][j + 1] = dp[i][j + 1].max(dp[i + 1][j]);
412+
}
413+
}
414+
(word1.len() + word2.len() - 2 * dp[word1.len()][word2.len()]) as i32
415+
}
416+
}
417+
```
374418

375419

376420
<p align="center">

0 commit comments

Comments
(0)

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