28. 实现strStr()

methon1

class Solution:
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        l = len(needle)
        for i in range(len(haystack)-l+1):
            if haystack[i:i+l] == needle:
                return i
        return -1 

methon2

class Solution:
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """   
        if needle  not in haystack:
            return -1
        return haystack.find(needle)

你可能感兴趣的:(28. 实现strStr())