class Solution: def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ # 法一 # n = len(haystack) # m = len(needle) # if not m or haystack == needle: # return 0 # if n < m: # return -1 # for i in range(n-m+1): # if haystack[i:i+m] == needle: # return i # return -1 # 法二 # if needle in haystack: # return haystack.index(needle) # elif not needle or haystack == needle: # return 0 # else: # return -1 # 法三 return haystack.find(needle)
以上三个方法,均测试通过。