1. 程式人生 > >python版Leetcode28 實現str

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