From eb9c4e647becea279c985e73fd2d4864c002b4a4 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 17:29:22 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200392.=E5=88=A4=E6=96=AD=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0392.判断子序列.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/problems/0392.判断子序列.md b/problems/0392.判断子序列.md index c10114c0..8bb078bd 100644 --- a/problems/0392.判断子序列.md +++ b/problems/0392.判断子序列.md @@ -258,9 +258,25 @@ func isSubsequence(s string, t string) bool { } ``` +Rust: - - +```rust +impl Solution { + pub fn is_subsequence(s: String, t: String) -> bool { + let mut dp = vec![vec![0; t.len() + 1]; s.len() + 1]; + for (i, char_s) in s.chars().enumerate() { + for (j, char_t) in t.chars().enumerate() { + if char_s == char_t { + dp[i + 1][j + 1] = dp[i][j] + 1; + continue; + } + dp[i + 1][j + 1] = dp[i + 1][j] + } + } + dp[s.len()][t.len()] == s.len() + } +} +```

From 503c83db8e9323fe4a94bd824a2b19ff617780d2 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sun, 23 Jul 2023 17:53:05 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200392.=E5=88=A4=E6=96=AD=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0392.判断子序列.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0392.判断子序列.md b/problems/0392.判断子序列.md index 8bb078bd..700d6825 100644 --- a/problems/0392.判断子序列.md +++ b/problems/0392.判断子序列.md @@ -278,6 +278,30 @@ impl Solution { } ``` +> 滚动数组 + +```rust +impl Solution { + pub fn is_subsequence(s: String, t: String) -> bool { + let mut dp = vec![0; t.len() + 1]; + let (s, t) = (s.as_bytes(), t.as_bytes()); + for &byte_s in s { + let mut pre = 0; + for j in 0..t.len() { + let temp = dp[j + 1]; + if byte_s == t[j] { + dp[j + 1] = pre + 1; + } else { + dp[j + 1] = dp[j]; + } + pre = temp; + } + } + dp[t.len()] == s.len() + } +} +``` +