implement_strstr

  • 2022-12-14
  • 浏览 (542)

implement_strstr.py 源码

# 实现 strStr()
# https://leetcode-cn.com/problems/implement-strstr


class Solution:

    # BF 算法,其他的几种太复杂了,暂时不去动(BM、KMP。。。)
    def strStr_1(self, haystack: str, needle: str) -> int:
        for i in range(len(haystack) - len(needle) + 1):
            j = 0
            while j < len(needle):
                if needle[j] != haystack[i + j]:
                    break
                j += 1
            if j == len(needle):
                return i
        return -1

你可能感兴趣的文章

first_unique_char

is_palindrome

length_of_ast_word

0  赞