LeetCode.28 - 实现strStr()

作者: 半亩房顶 | 来源:发表于2019-05-28 19:42 被阅读0次

    题目

    实现 strStr() 函数。

    给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1

    示例 1:
    输入: haystack = "hello", needle = "ll"
    输出: 2

    示例 2:
    输入: haystack = "aaaaa", needle = "bba"
    输出: -1

    说明:

    needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

    对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

    思路

    这个题乍一看,难度是简单?
    最熟悉的字符串匹配,莫过于经典的KMP了,理解透彻next数组怎么说也不能算是简单了吧?不过很快发现了简单的所在,可以使用内置函数,这样一来,貌似就是一个优雅与否的问题了
    不过,抱着这样的态度,显然不太行,因为提高自己才是我们想要的
    看了下大神们的答案,我发现了BM算法 和 Sunday算法
    这里简单说下自己对Sunday算法的理解吧,详细的可以戳上面的链接看下:

    • 从左往右遍历对比

    • 关注主串中参与匹配的最末位字符后面的那一位(下图蓝色位置)

    • 两种情况如图:


      Case1 & 2
    • 缺点是子串中大量重复时,效率变差最差O(m*n)

    再重复下,详细请戳链接 BM算法 和 Sunday算法

    自写代码

    暴力版

    class Solution:
       def strStr(self, haystack: str, needle: str) -> int:
           if len(needle) == 0:
               return 0
           if len(haystack) == 0:
               return -1
           #不用内置函数,直接以needle的长度依次遍历
           for i in range(len(haystack)):
               #真的,不要在循环套循环了,你不像个写py的
               if haystack[i:i+len(needle)] == needle:
                   return i
           return -1
    
    

    经典的KMP算法

        def kmp_search(self, haystack: str, needle: str) -> int:
    
            def get_next(pattern: str):
                j, k, l = 0, -1, len(pattern)
    
                p_next = [-1 for _ in range(l)]
    
                while j < l - 1:
                    if k == -1 or pattern[j] == pattern[k]:
                        k += 1
                        j += 1
                        if pattern[j] != pattern[k]:
                            p_next[j] = k
                        else:
                            p_next[j] = p_next[k]
                    else:
                        k = p_next[k]
    
                return p_next
    
            i, j, h_l, n_l = 0, 0, len(haystack), len(needle)
            p_next = get_next(needle)
    
            while i < h_l and j < n_l:
                if j == -1 or haystack[i] == needle[j]:
                    i += 1
                    j += 1
                else:
                    j = p_next[j]
    
            if j == n_l:
                return i - j
            return -1
    

    优雅代码

    能两行不来三行:

    class Solution:
        def strStr(self, haystack: str, needle: str) -> int:
            if not needle:return 0
            return haystack.index(needle) if needle in haystack else -1
    

    sunday算法

    def sunday_search(self, haystack: str, needle: str) -> int:
            if not needle:
                return 0
    
            offset = {}
            n_l = len(needle)
    
            for i in range(n_l):
                offset[needle[i]] = n_l - i
    
            i, j, h_l = 0, 0, len(haystack)
    
            while i <= h_l - n_l:
                j = 0
    
                while haystack[i + j] == needle[j]:
                    j += 1
                    if j == n_l:
                        return i
    
                if i + n_l == h_l:
                    return -1
    
                if haystack[i + n_l] in offset:
                    i += offset[haystack[i + n_l]]
                else:
                    i += n_l + 1
    
            return -1
    

    自测反思

    • 当 needle 是空字符串时,我们应当返回什么值呢?
      这句话在题目中给了提示,而这个确实是一个很好的问题,边界,永远是测试重点。
    • 多个存在,会不会出现问题?
    • 不存在,会不会有问题?

    以上,欢迎大家讨论,共同进步


    欢迎大家关注我的公众号


    半亩房顶

    相关文章

      网友评论

        本文标题:LeetCode.28 - 实现strStr()

        本文链接:https://www.haomeiwen.com/subject/eguktctx.html