28. Implement strStr()
problem
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
求第二个字符串在第一个字符串中的位置
注意,字符串不定长
solution
range(-100) = []
import bisect
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
step = len(needle)
for j in range(len(haystack)-len(needle)+1):
if haystack[j:j+step] == needle:
return j
return -1

浙公网安备 33010602011771号