Leetcode刷题记录——28. 实现 strStr()

在这里插入图片描述

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        
        lenh = len(haystack)
        lenn = len(needle)
        if lenh == lenn and haystack == needle:
            return 0
        elif lenh < lenn:
            return -1
        checkrange = lenh - lenn + 1
        for i in range(checkrange):
            if haystack[i:i+lenn] == needle:
                return i
        else:
            return -1
发布了59 篇原创文章 · 获赞 14 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_41545780/article/details/105478992