Leetcode 28: Implement strStr()

题目:
Leetcode 28: Implement strStr()_第1张图片
python3代码:

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        if (len(haystack) == 0 and len(needle) == 0) or (len(haystack) != 0 and len(needle) == 0):
            return 0
        if len(haystack) < len(needle):
            return -1
        for i in range(len(haystack)-len(needle)+1):
            if haystack[i:i+len(needle)] == needle:
                return i
        return -1

Leetcode 28: Implement strStr()_第2张图片

如果觉得不错,就点赞或者关注或者留言~
谢谢~

你可能感兴趣的:(Leetcode,python,leetcode,算法,字符串,28)