implement_strstr

2022-12-14 浏览 (703)

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

  • 所属分类: 后端技术
  • 本文标签: 技术
  • 版权声明: 本文链接 https://seaxiang.com/blog/195967611d9247bdb43b70a0204e5431