diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index 5cc04994..ff13db39 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -692,107 +692,66 @@ class Solution { ``` Python3: -(版本一)前缀表(不减一) +(版本一)前缀表(减一) +```python +class Solution: + def getNext(self, next, s): + j = -1 + next[0] = j + for i in range(1, len(s)): + while j >= 0 and s[i] != s[j+1]: + j = next[j] + if s[i] == s[j+1]: + j += 1 + next[i] = j + + def strStr(self, haystack: str, needle: str) -> int: + if not needle: + return 0 + next = [0] * len(needle) + self.getNext(next, needle) + j = -1 + for i in range(len(haystack)): + while j >= 0 and haystack[i] != needle[j+1]: + j = next[j] + if haystack[i] == needle[j+1]: + j += 1 + if j == len(needle) - 1: + return i - len(needle) + 1 + return -1 +``` +(版本二)前缀表(不减一) ```python class Solution: + def getNext(self, next: List[int], s: str) -> None: + j = 0 + next[0] = 0 + for i in range(1, len(s)): + while j > 0 and s[i] != s[j]: + j = next[j - 1] + if s[i] == s[j]: + j += 1 + next[i] = j + def strStr(self, haystack: str, needle: str) -> int: if len(needle) == 0: return 0 - next = self.getNext(needle) + next = [0] * len(needle) + self.getNext(next, needle) j = 0 for i in range(len(haystack)): - while j >= 1 and haystack[i] != needle[j]: - j = next[j-1] + while j > 0 and haystack[i] != needle[j]: + j = next[j - 1] if haystack[i] == needle[j]: j += 1 if j == len(needle): return i - len(needle) + 1 return -1 - - def getNext(self, needle): - next = [0] * len(needle) - j = 0 - next[0] = j - for i in range(1, len(needle)): - while j >= 1 and needle[i] != needle[j]: - j = next[j-1] - if needle[i] == needle[j]: - j += 1 - next[i] = j - return next -``` -(版本二)前缀表(减一) -```python -class Solution: - def strStr(self, haystack: str, needle: str) -> int: - a = len(needle) - b = len(haystack) - if a == 0: - return 0 - next = self.getnext(a,needle) - p=-1 - for j in range(b): - while p >= 0 and needle[p+1] != haystack[j]: - p = next[p] - if needle[p+1] == haystack[j]: - p += 1 - if p == a-1: - return j-a+1 - return -1 - - def getnext(self,a,needle): - next = ['' for i in range(a)] - k = -1 - next[0] = k - for i in range(1, len(needle)): - while (k > -1 and needle[k+1] != needle[i]): - k = next[k] - if needle[k+1] == needle[i]: - k += 1 - next[i] = k - return next ``` -(版本三)前缀表(减一) - - -```python -class Solution: - def strStr(self, haystack: str, needle: str) -> int: - a = len(needle) - b = len(haystack) - if a == 0: - return 0 - i = j = 0 - next = self.getnext(a, needle) - while(i < b and j < a): - if j == -1 or needle[j] == haystack[i]: - i += 1 - j += 1 - else: - j = next[j] - if j == a: - return i-j - else: - return -1 - - def getnext(self, a, needle): - next = ['' for i in range(a)] - j, k = 0, -1 - next[0] = k - while(j < a-1): - if k == -1 or needle[k] == needle[j]: - k += 1 - j += 1 - next[j] = k - else: - k = next[k] - return next -``` - -(版本四)暴力法 +(版本三)暴力法 ```python class Solution(object): def strStr(self, haystack, needle): @@ -807,7 +766,7 @@ class Solution(object): return i return -1 ``` -(版本五)使用 index +(版本四)使用 index ```python class Solution: def strStr(self, haystack: str, needle: str) -> int: @@ -816,7 +775,7 @@ class Solution: except ValueError: return -1 ``` -(版本六)使用 find +(版本五)使用 find ```python class Solution: def strStr(self, haystack: str, needle: str) -> int: