找出字符串中第一个匹配项的下标
def strStr(haystack: str, needle: str) -> int:
return haystack.find(needle)
啊这…
或者:
def strStr(haystack: str, needle: str) -> int:
length1 = len(haystack)
length2 = len(needle)
for i in range(length1 - length2 + 1):
if haystack[i] == needle[0]:
if haystack[i:i + length2] == needle:
return i
else:
continue
return -1
看题解里面很多说用KMP算法
还不会,等期末考完再来填坑