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 1f50fa2

Browse files
author
ruislan
committed
solved q583
1 parent 346af2d commit 1f50fa2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

‎src/q/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ mod q574;
288288
mod q575;
289289
mod q576;
290290
mod q581;
291+
mod q583;
291292
mod q589;
292293
mod q590;
293294
mod q594;

‎src/q/q583.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crate::q::Solution;
2+
3+
#[allow(unused)]
4+
impl Solution {
5+
pub fn min_distance(word1: String, word2: String) -> i32 {
6+
// 方法1
7+
// 这个题只用了删除,而没有替换或者插入操作
8+
// 那么也就是意味着,我们只需要找出最长公共子序列(LCS)就可以了
9+
// 这样保证双方保留的字符是最多的剩下的就是删除的
10+
// 不过,如果有替换和插入操作,那么我们就需要使用编辑距离的解法了(Edit distance)
11+
// AC 4ms 4.1mb 1306/1306
12+
let n = word1.len();
13+
let m = word2.len();
14+
let word1: Vec<char> = word1.chars().collect();
15+
let word2: Vec<char> = word2.chars().collect();
16+
let mut dp = vec![vec![0; m + 1]; n + 1];
17+
for i in 1..=n {
18+
for j in 1..=m {
19+
if word1[i - 1] == word2[j - 1] {
20+
dp[i][j] = dp[i - 1][j - 1] + 1;
21+
} else {
22+
dp[i][j] = dp[i - 1][j].max(dp[i][j - 1]);
23+
}
24+
}
25+
}
26+
(n + m - dp[n][m] * 2) as i32
27+
}
28+
}

0 commit comments

Comments
(0)

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