From a7e35954b094020f7813d90d83a5d7f6ec869a23 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 24 Jul 2023 22:37:56 +0800 Subject: [PATCH] =?UTF-8?q?Update=200072.=E7=BC=96=E8=BE=91=E8=B7=9D?= =?UTF-8?q?=E7=A6=BB.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0072.编辑距离.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0072.编辑距离.md b/problems/0072.编辑距离.md index 703e8913..19e7aade 100644 --- a/problems/0072.编辑距离.md +++ b/problems/0072.编辑距离.md @@ -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 + } +} +``` +