mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Update 0028.实现strStr.md
This commit is contained in:
@ -692,23 +692,38 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python3:
|
Python3:
|
||||||
|
(版本一)前缀表(不减一)
|
||||||
|
|
||||||
```python
|
```python
|
||||||
//暴力解法:
|
class Solution:
|
||||||
class Solution(object):
|
def strStr(self, haystack: str, needle: str) -> int:
|
||||||
def strStr(self, haystack, needle):
|
if len(needle) == 0:
|
||||||
"""
|
return 0
|
||||||
:type haystack: str
|
next = self.getNext(needle)
|
||||||
:type needle: str
|
j = 0
|
||||||
:rtype: int
|
for i in range(len(haystack)):
|
||||||
"""
|
while j >= 1 and haystack[i] != needle[j]:
|
||||||
m, n = len(haystack), len(needle)
|
j = next[j-1]
|
||||||
for i in range(m):
|
if haystack[i] == needle[j]:
|
||||||
if haystack[i:i+n] == needle:
|
j += 1
|
||||||
return i
|
if j == len(needle):
|
||||||
|
return i - len(needle) + 1
|
||||||
return -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
|
```python
|
||||||
// 方法一
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def strStr(self, haystack: str, needle: str) -> int:
|
def strStr(self, haystack: str, needle: str) -> int:
|
||||||
a = len(needle)
|
a = len(needle)
|
||||||
@ -739,8 +754,11 @@ class Solution:
|
|||||||
return next
|
return next
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
(版本三)前缀表(减一)
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
// 方法二
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def strStr(self, haystack: str, needle: str) -> int:
|
def strStr(self, haystack: str, needle: str) -> int:
|
||||||
a = len(needle)
|
a = len(needle)
|
||||||
@ -774,35 +792,36 @@ class Solution:
|
|||||||
return next
|
return next
|
||||||
```
|
```
|
||||||
|
|
||||||
|
(版本四)暴力法
|
||||||
|
```python
|
||||||
|
class Solution(object):
|
||||||
|
def strStr(self, haystack, needle):
|
||||||
|
"""
|
||||||
|
:type haystack: str
|
||||||
|
:type needle: str
|
||||||
|
:rtype: int
|
||||||
|
"""
|
||||||
|
m, n = len(haystack), len(needle)
|
||||||
|
for i in range(m):
|
||||||
|
if haystack[i:i+n] == needle:
|
||||||
|
return i
|
||||||
|
return -1
|
||||||
|
```
|
||||||
|
(版本五)使用 index
|
||||||
```python
|
```python
|
||||||
// 前缀表(不减一)Python实现
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def strStr(self, haystack: str, needle: str) -> int:
|
def strStr(self, haystack: str, needle: str) -> int:
|
||||||
if len(needle) == 0:
|
try:
|
||||||
return 0
|
return haystack.index(needle)
|
||||||
next = self.getNext(needle)
|
except ValueError:
|
||||||
j = 0
|
|
||||||
for i in range(len(haystack)):
|
|
||||||
while j >= 1 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
|
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
|
|
||||||
```
|
```
|
||||||
|
(版本六)使用 find
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def strStr(self, haystack: str, needle: str) -> int:
|
||||||
|
return haystack.find(needle)
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user