mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Merge pull request #1520 from cezarbbb/String01
添加 0028.实现strStr 0459.重复的子字符串 Rust版本
This commit is contained in:
@ -1241,5 +1241,49 @@ function getNext(&$next, $s){
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Rust:
|
||||
|
||||
> 前缀表统一不减一
|
||||
```Rust
|
||||
impl Solution {
|
||||
pub fn get_next(next: &mut Vec<usize>, s: &Vec<char>) {
|
||||
let len = s.len();
|
||||
let mut j = 0;
|
||||
for i in 1..len {
|
||||
while j > 0 && s[i] != s[j] {
|
||||
j = next[j - 1];
|
||||
}
|
||||
if s[i] == s[j] {
|
||||
j += 1;
|
||||
}
|
||||
next[i] = j;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn str_str(haystack: String, needle: String) -> i32 {
|
||||
let (haystack_len, needle_len) = (haystack.len(), needle.len());
|
||||
if haystack_len == 0 { return 0; }
|
||||
if haystack_len < needle_len { return -1;}
|
||||
let (haystack, needle) = (haystack.chars().collect::<Vec<char>>(), needle.chars().collect::<Vec<char>>());
|
||||
let mut next: Vec<usize> = vec![0; haystack_len];
|
||||
Self::get_next(&mut next, &needle);
|
||||
let mut j = 0;
|
||||
for i in 0..haystack_len {
|
||||
while j > 0 && haystack[i] != needle[j] {
|
||||
j = next[j - 1];
|
||||
}
|
||||
if haystack[i] == needle[j] {
|
||||
j += 1;
|
||||
}
|
||||
if j == needle_len {
|
||||
return (i - needle_len + 1) as i32;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -505,5 +505,37 @@ Swift:
|
||||
}
|
||||
```
|
||||
|
||||
Rust:
|
||||
|
||||
>前缀表统一不减一
|
||||
```Rust
|
||||
impl Solution {
|
||||
pub fn get_next(next: &mut Vec<usize>, s: &Vec<char>) {
|
||||
let len = s.len();
|
||||
let mut j = 0;
|
||||
for i in 1..len {
|
||||
while j > 0 && s[i] != s[j] {
|
||||
j = next[j - 1];
|
||||
}
|
||||
if s[i] == s[j] {
|
||||
j += 1;
|
||||
}
|
||||
next[i] = j;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn repeated_substring_pattern(s: String) -> bool {
|
||||
let s = s.chars().collect::<Vec<char>>();
|
||||
let len = s.len();
|
||||
if len == 0 { return false; };
|
||||
let mut next = vec![0; len];
|
||||
Self::get_next(&mut next, &s);
|
||||
if next[len - 1] != 0 && len % (len - (next[len - 1] )) == 0 { return true; }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user