python版Leetcode28 实现str

在这里插入图片描述

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

猜你喜欢

转载自blog.csdn.net/weixin_38278334/article/details/83627717