zoukankan      html  css  js  c++  java
  • leetcode28. 实现strStr() 🌟

    题目:

      实现 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() 定义相符。

    来源:力扣(LeetCode)
    解答:

    leetcode优秀方案(来自力扣答案统计页,没有明确作者是谁,可留言告知):

    class Solution:
        def strStr(self, haystack: str, needle: str) -> int:
            for i in range(len(haystack) - len(needle) + 1):
                if haystack[i: i + len(needle)] == needle:
                    return i
            return -1
    View Code
    class Solution:
        def strStr(self, haystack: str, needle: str) -> int:
            if needle == '':
                return 0
    
            i = 0
            while i <= len(haystack) - len(needle):
                if haystack[i] != needle[0]:
                    i += 1
                else:
                    j = i + 1
                    k = 1
                    while k < len(needle):
                        if haystack[j] == needle[k]:
                            j += 1
                            k += 1
                        else:
                            i += 1
                            break
                    else:
                        return i
            else:
                return -1
    View Code
    class Solution:
        def strStr(self, haystack: str, needle: str) -> int:
            if len(needle) == 0:
                return 0
            i = 0
            j = 0
            while i < len(haystack) and j < len(needle):
                if haystack[i] == needle[j]:
                    i += 1
                    j += 1
                else:
                    i = i - j + 1
                    j = 0
            if j == len(needle):
                return i - j
            return -1
        # https://leetcode-cn.com/problems/implement-strstr/comments/53265
    View Code
    class Solution:
        def strStr(self, haystack: str, needle: str) -> int:
            if needle == '': return 0
            for i in range(len(haystack) - len(needle) + 1):
                if haystack[i] == needle[0]:
                    first = ''
                    for j in range(len(needle)):
                        first += haystack[i+j]
                    if first == needle:
                        return i
            
            return -1
    
    # 作者:jian-chuan
    # 链接:https://leetcode-cn.com/problems/two-sum/solution/nei-zhi-han-shu-jiu-bu-xie-liao-lai-ge-bao-li-po-j/
    View Code
    class Solution:
        def strStr(self, haystack: str, needle: str) -> int:
            if not needle : return 0
            _next = [-1]
            
            def getNext(p, _next):
                _next[0] = -1
                i = 0
                j = -1
                while i < len(p) - 1:
                    if j == -1 or p[i] == p[j]:
                        i += 1
                        j += 1
                        # _next.append(j)
                        if p[i] != p[j]:
                            _next.append(j)
                        else:
                            _next.append(_next[j])
                    else:
                        j = _next[j]
            getNext(needle, _next)
            
            i = 0
            j = 0
            while i < len(haystack) and j < len(needle):
                if j == -1 or haystack[i] == needle[j]:
                    i += 1
                    j += 1
                else:
                    j = _next[j]
            if j == len(needle):
                return i - j
            return -1
    
    # 作者:powcai
    # 链接:https://leetcode-cn.com/problems/two-sum/solution/shi-xian-strstr-by-powcai/
    # 链接:https://leetcode-cn.com/problems/implement-strstr/solution/c5chong-jie-fa-ku-han-shu-bfkmpbmsunday-by-2227
    View Code
    class Solution:
        def strStr(self, haystack: str, needle: str) -> int:
            def get_bmB(T, bmB):
                tlen = len(T)
                for i in range(256):
                    bmB.append(len(T))
                for i in range(tlen - 1):
                    bmB[ord(T[i])] = tlen - i - 1
            
            def get_suff(T, suff):
                tlen = len(T)
                for i in range(tlen - 2 , -1 , -1):
                    k = i
                    while k > 0 and T[k] == T[tlen - 1 - i + k]:
                        k -= 1
                    suff[i] = i - k
            
            def get_bmG(T, bmG):
                tlen = len(T)
                suff = [0] * (tlen + 1)
                get_suff(T, suff)
                for i in range(tlen):
                    bmG[i] = tlen
                for i in range(tlen - 1, -1, -1):
                    if suff[i] == i + 1:
                        for j in range(tlen - 1):
                            if bmG[j] == tlen:
                                bmG[j] = tlen - 1 - i
                for i in range(tlen - 1):
                    bmG[tlen - 1 - suff[i]] = tlen - 1 - i
            
            i = 0
            tlen = len(needle)
            slen = len(haystack)
            bmG = [0] * tlen
            bmB = []
            get_bmB(needle, bmB)
            get_bmG(needle, bmG)
            
            while i <= slen - tlen:
                j = tlen - 1
                while j > -1 and haystack[i + j] == needle[j]:
                        j -= 1
                if j == -1:
                    return i
                
                i += max(bmG[j], bmB[ord(haystack[i+j])] - (tlen - 1 - j))
            
            return -1
    
    # https://leetcode-cn.com/problems/implement-strstr/solution/c5chong-jie-fa-ku-han-shu-bfkmpbmsunday-by-2227
    View Code
    class Solution:
        def strStr(self, haystack: str, needle: str) -> int:
            if needle == '': return 0
            
            slen = len(haystack)
            tlen = len(needle)
            if slen < tlen: return -1
            
            i = j = 0
            m = tlen
            
            while i < slen:
                if haystack[i] != needle[j]:
                    for k in range(tlen - 1, -1, -1):
                        if m < slen and needle[k] == haystack[m]:
                            break
                    i = m - k
                    j = 0
                    m = i + tlen
                    if m > slen:
                        return -1
                else:
                    if j == tlen - 1:
                        return i - j
                    i += 1
                    j += 1
            
            return -1
    
    # https://leetcode-cn.com/problems/implement-strstr/solution/c5chong-jie-fa-ku-han-shu-bfkmpbmsunday-by-2227
    View Code

    字符串匹配的KMP算法
    字符串匹配的Boyer-Moore算法

  • 相关阅读:
    Shiro
    Python活力练习Day11
    Python活力练习Day10
    Python活力练习Day9
    数据框DataFrame和列表List相互转换
    Python活力练习Day8
    Python中绘制箭头
    Python活力练习Day7
    Python活力练习Day6
    Python活力练习Day5
  • 原文地址:https://www.cnblogs.com/catyuang/p/11125984.html
Copyright © 2011-2022 走看看