From 59c86a7023989e3cb3195615f30a0a99e39fa556 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Wed, 29 Mar 2023 18:15:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200844.=E6=AF=94=E8=BE=83=E5=90=AB?= =?UTF-8?q?=E9=80=80=E6=A0=BC=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0844.比较含退格的字符串.md | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0844.比较含退格的字符串.md b/problems/0844.比较含退格的字符串.md index 6534f18c..40d5caa1 100644 --- a/problems/0844.比较含退格的字符串.md +++ b/problems/0844.比较含退格的字符串.md @@ -535,6 +535,35 @@ function getIndexAfterDel(s: string, startIndex: number): number { } ``` +### Rust + +> 双指针 + +```rust +impl Solution { + pub fn backspace_compare(s: String, t: String) -> bool { + let (s, t) = ( + s.chars().collect::>(), + t.chars().collect::>(), + ); + Self::get_string(s) == Self::get_string(t) + } + + pub fn get_string(mut chars: Vec) -> Vec { + let mut slow = 0; + for i in 0..chars.len() { + if chars[i] == '#' { + slow = (slow as u32).saturating_sub(1) as usize; + } else { + chars[slow] = chars[i]; + slow += 1; + } + } + chars.truncate(slow); + chars + } +} +``` From e4951bc9dba7ebaa5314d41a52b0a5d97b8a6cea Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Wed, 29 Mar 2023 18:34:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200844.=E6=AF=94=E8=BE=83=E5=90=AB?= =?UTF-8?q?=E9=80=80=E6=A0=BC=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0844.比较含退格的字符串.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0844.比较含退格的字符串.md b/problems/0844.比较含退格的字符串.md index 40d5caa1..ac56f6f9 100644 --- a/problems/0844.比较含退格的字符串.md +++ b/problems/0844.比较含退格的字符串.md @@ -565,7 +565,27 @@ impl Solution { } ``` +> 双栈法 +```rust +impl Solution { + pub fn backspace_compare(s: String, t: String) -> bool { + Self::get_string(s) == Self::get_string(t) + } + + pub fn get_string(string: String) -> String { + let mut s = String::new(); + for c in string.chars() { + if c != '#' { + s.push(c); + } else if !s.is_empty() { + s.pop(); + } + } + s + } +} +```