python LeetCode28:实现strStr

题目:

"""
给你两个字符串 haystack 和 needle ,
请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。
"""
class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        if haystack==needle=='':
            return 0
        length = len(needle)
        for i in range(len(haystack)):
            if haystack[i:i+length] == needle:
                return i
        return -1

haystack = ""
needle = ""
S = Solution()
result = S.strStr(haystack,needle)
print(result)

你可能感兴趣的:(算法,leetcode,算法,职场和发展)