diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index 697e8c2d..19d16e9f 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -1303,7 +1303,8 @@ impl Solution { ```rust impl Solution { - pub fn get_next(mut next: Vec, s: &Vec) -> Vec { + pub fn get_next(next_len: usize, s: &Vec) -> Vec { + 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::>(), ); 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];