Update 0028.实现strStr.md

This commit is contained in:
fw_qaq
2022-10-25 22:35:54 +08:00
committed by GitHub
parent 84b4f26744
commit a582640c7a

View File

@ -1303,7 +1303,8 @@ impl Solution {
```rust
impl Solution {
pub fn get_next(mut next: Vec<i32>, s: &Vec<char>) -> Vec<i32> {
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[(j + 1) as usize] != s[i] {
@ -1328,8 +1329,7 @@ impl Solution {
needle.chars().collect::<Vec<char>>(),
);
let mut j = -1;
let mut next = vec![-1; needle.len()];
next = Self::get_next(next, &needle_chars);
let next = Self::get_next(needle.len(), &needle_chars);
for (i, v) in haystack_chars.into_iter().enumerate() {
while j >= 0 && v != needle_chars[(j + 1) as usize] {
j = next[j as usize];