leetcode 28 str()
使用朴素匹配方法。
class Solution: def strStr(self, haystack: str, needle: str) -> int: if haystack=="" and needle!="": return -1 if needle=="": return 0 haystack=list(haystack) flag=True i=0 while i+len(needle)<=len(haystack): if haystack[i]==needle[0]: flag=True for j in range(1,len(needle)): if needle[j]!=haystack[i+j]: flag=False break if flag: return i i+=1 return -1