From 69ad28ca0ecbcd726619d5aad81351d4e275db54 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 24 Jul 2023 23:57:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200647.=E5=9B=9E=E6=96=87=E5=AD=90?= =?UTF-8?q?=E4=B8=B2.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0647.回文子串.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0647.回文子串.md b/problems/0647.回文子串.md index 084b9f74..35396192 100644 --- a/problems/0647.回文子串.md +++ b/problems/0647.回文子串.md @@ -517,8 +517,33 @@ function expandRange(s: string, left: number, right: number): number { } ``` +Rust: +> 双指针 + +```rust +impl Solution { + pub fn count_substrings(s: String) -> i32 { + let mut res = 0; + for i in 0..s.len() { + res += Self::extend(&s, i, i, s.len()); + res += Self::extend(&s, i, i + 1, s.len()); + } + res + } + + fn extend(s: &str, mut i: usize, mut j: usize, len: usize) -> i32 { + let mut res = 0; + while i < len && j < len && s[i..=i] == s[j..=j] { + res += 1; + i = i.wrapping_sub(1); + j += 1; + } + res + } +} +```

From 6011184b37f9e1d661edef1b124b5e195ef55bd9 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 25 Jul 2023 00:18:19 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200647.=E5=9B=9E=E6=96=87=E5=AD=90?= =?UTF-8?q?=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0647.回文子串.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0647.回文子串.md b/problems/0647.回文子串.md index 35396192..52f12d9b 100644 --- a/problems/0647.回文子串.md +++ b/problems/0647.回文子串.md @@ -519,6 +519,24 @@ function expandRange(s: string, left: number, right: number): number { Rust: +```rust +impl Solution { + pub fn count_substrings(s: String) -> i32 { + let mut dp = vec![vec![false; s.len()]; s.len()]; + let mut res = 0; + + for i in (0..s.len()).rev() { + for j in i..s.len() { + if s[i..=i] == s[j..=j] && (j - i <= 1 || dp[i + 1][j - 1]) { + dp[i][j] = true; + res += 1; + } + } + } + res + } +} +``` > 双指针