classSolution:defstrStr(self, haystack:str, needle:str)->int:
k =len(needle)
n =len(haystack)# for i in range(n):for i inrange(n-k+1):#边界if haystack[i:i + k]== needle:return i
return-1
Leetcode28-实现 strStr()思路:切片。遍历。class Solution: def strStr(self, haystack: str, needle: str) -> int: k = len(needle) n = len(haystack) # for i in range(n): for i in range(n-k+1): #边界 if haystack[i:i + k]