mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
update 0459.重复的子字符串.md about rust 前缀表统一 -1
This commit is contained in:
@ -615,6 +615,35 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
>前缀表统一减一
|
||||||
|
|
||||||
|
```rust
|
||||||
|
pub fn get_next(next_len: usize, s: &Vec<char>) -> Vec<i32> {
|
||||||
|
let mut next = vec![-1; next_len];
|
||||||
|
let mut j = -1;
|
||||||
|
for i in 1..s.len() {
|
||||||
|
while j >= 0 && s[i] != s[(j + 1) as usize] {
|
||||||
|
j = next[j as usize];
|
||||||
|
}
|
||||||
|
if s[i] == s[(j + 1) as usize] {
|
||||||
|
j += 1;
|
||||||
|
}
|
||||||
|
next[i] = j;
|
||||||
|
}
|
||||||
|
next
|
||||||
|
}
|
||||||
|
pub fn repeated_substring_pattern(s: String) -> bool {
|
||||||
|
let s_chars = s.chars().collect::<Vec<char>>();
|
||||||
|
let next = Self::get_next(s_chars.len(), &s_chars);
|
||||||
|
if next[s_chars.len() - 1] >= 0
|
||||||
|
&& s_chars.len() % (s_chars.len() - (next[s_chars.len() - 1] + 1) as usize) == 0
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
<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