python LeetCode28:实现strStr

python LeetCode28:实现strStr,第1张

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)

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5651013.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存