mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #2203 from fwqaaq/master
Update 0647.回文子串.md about rust
This commit is contained in:
@ -521,8 +521,51 @@ 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> 双指针
|
||||||
|
|
||||||
|
```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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
Reference in New Issue
Block a user