leetcode_28. 实现strStr()python

解题思路:

1.可以选择暴力破解,也可采用KMP算法,KMP算法以后再研究
2.切片的方法,这个切片的方法就很新颖了,就是在needle部分将这个字符串切片,然后找到第一部分的长度即可,这个方法很有意思

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

猜你喜欢

转载自blog.csdn.net/qq_37002901/article/details/87916262