【LeetCode】28. Implement strStr()

class Solution_1(object):
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        if not needle:
            return 0
        if needle not in haystack:
            return -1
        return len(haystack.split(needle)[0])


class Solution_2(object):
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        if not needle:
            return 0
        if needle not in haystack:
            return -1
        l = len(haystack)
        ll = len(needle)
        for ids in range(l):
            if ids + ll > l:
                return -1
            if haystack[ids: ids+ll] == needle:
                return ids

猜你喜欢

转载自blog.csdn.net/zzc15806/article/details/81191316