diff --git "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" index d925c5dede..0d2477bdf4 100644 --- "a/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" +++ "b/problems/0115.\344\270\215\345\220\214\347\232\204\345\255\220\345\272\217\345\210\227.md" @@ -314,6 +314,63 @@ function numDistinct(s: string, t: string): number { }; ``` +### Rust: + +```rust +impl Solution { + pub fn num_distinct(s: String, t: String) -> i32 { + if s.len() < t.len() { + return 0; + } + let mut dp = vec![vec![0; s.len() + 1]; t.len() + 1]; + // i = 0, t 为空字符串,s 作为子序列的个数为 1(删除 s 所有元素) + dp[0] = vec![1; s.len() + 1]; + for (i, char_t) in t.chars().enumerate() { + for (j, char_s) in s.chars().enumerate() { + if char_t == char_s { + // t 的前 i 个字符在 s 的前 j 个字符中作为子序列的个数 + dp[i + 1][j + 1] = dp[i][j] + dp[i + 1][j]; + continue; + } + dp[i + 1][j + 1] = dp[i + 1][j]; + } + } + dp[t.len()][s.len()] + } +} +``` + +> 滚动数组 + +```rust +impl Solution { + pub fn num_distinct(s: String, t: String) -> i32 { + if s.len() < t.len() { + return 0; + } + let (s, t) = (s.into_bytes(), t.into_bytes()); + // 对于 t 为空字符串,s 作为子序列的个数为 1(删除 s 所有元素) + let mut dp = vec![1; s.len() + 1]; + for char_t in t { + // dp[i - 1][j - 1],dp[j + 1] 更新之前的值 + let mut pre = dp[0]; + // 当开始遍历 t,s 的前 0 个字符无法包含任意子序列 + dp[0] = 0; + for (j, &char_s) in s.iter().enumerate() { + let temp = dp[j + 1]; + if char_t == char_s { + dp[j + 1] = pre + dp[j]; + } else { + dp[j + 1] = dp[j]; + } + pre = temp; + } + } + dp[s.len()] + } +} +``` +

diff --git "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" index 505a4e33ee..7dbb8ef542 100644 --- "a/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" +++ "b/problems/0583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234.md" @@ -370,7 +370,51 @@ function minDistance(word1: string, word2: string): number { }; ``` +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 0..word1.len() { + dp[i + 1][0] = i + 1; + } + for j in 0..word2.len() { + dp[0][j + 1] = j + 1; + } + 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]) + 1; + } + } + dp[word1.len()][word2.len()] as i32 + } +} +``` +> 版本 2 + +```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, char1) in word1.chars().enumerate() { + for (j, char2) in word2.chars().enumerate() { + if char1 == char2 { + dp[i + 1][j + 1] = dp[i][j] + 1; + continue; + } + dp[i + 1][j + 1] = dp[i][j + 1].max(dp[i + 1][j]); + } + } + (word1.len() + word2.len() - 2 * dp[word1.len()][word2.len()]) as i32 + } +} +```