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优秀方案(来自力扣答案统计页,没有明确作者是谁,可留言告知):

1 class Solution:
2     def strStr(self, haystack: str, needle: str) -> int:
3         for i in range(len(haystack) - len(needle) + 1):
4             if haystack[i: i + len(needle)] == needle:
5                 return i
6         return -1
 1 class Solution:
 2     def strStr(self, haystack: str, needle: str) -> int:
 3         if needle == '':
 4             return 0
 5 
 6         i = 0
 7         while i <= len(haystack) - len(needle):
 8             if haystack[i] != needle[0]:
 9                 i += 1
10             else:
11                 j = i + 1
12                 k = 1
13                 while k < len(needle):
14                     if haystack[j] == needle[k]:
15                         j += 1
16                         k += 1
17                     else:
18                         i += 1
19                         break
20                 else:
21                     return i
22         else:
23             return -1

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

leetcode其他优秀思路:

 1 class Solution:
 2     def strStr(self, haystack: str, needle: str) -> int:
 3         if needle == '': return 0
 4         for i in range(len(haystack) - len(needle) + 1):
 5             if haystack[i] == needle[0]:
 6                 first = ''
 7                 for j in range(len(needle)):
 8                     first += haystack[i+j]
 9                 if first == needle:
10                     return i
11         
12         return -1
13 
14 # 作者:jian-chuan
15 # 链接:https://leetcode-cn.com/problems/two-sum/solution/nei-zhi-han-shu-jiu-bu-xie-liao-lai-ge-bao-li-po-j/
 1 class Solution:
 2     def strStr(self, haystack: str, needle: str) -> int:
 3         if not needle : return 0
 4         _next = [-1]
 5         
 6         def getNext(p, _next):
 7             _next[0] = -1
 8             i = 0
 9             j = -1
10             while i < len(p) - 1:
11                 if j == -1 or p[i] == p[j]:
12                     i += 1
13                     j += 1
14                     # _next.append(j)
15                     if p[i] != p[j]:
16                         _next.append(j)
17                     else:
18                         _next.append(_next[j])
19                 else:
20                     j = _next[j]
21         getNext(needle, _next)
22         
23         i = 0
24         j = 0
25         while i < len(haystack) and j < len(needle):
26             if j == -1 or haystack[i] == needle[j]:
27                 i += 1
28                 j += 1
29             else:
30                 j = _next[j]
31         if j == len(needle):
32             return i - j
33         return -1
34 
35 # 作者:powcai
36 # 链接:https://leetcode-cn.com/problems/two-sum/solution/shi-xian-strstr-by-powcai/
37 # 链接:https://leetcode-cn.com/problems/implement-strstr/solution/c5chong-jie-fa-ku-han-shu-bfkmpbmsunday-by-2227

# 字符串匹配的KMP算法
 1 class Solution:
 2     def strStr(self, haystack: str, needle: str) -> int:
 3         def get_bmB(T, bmB):
 4             tlen = len(T)
 5             for i in range(256):
 6                 bmB.append(len(T))
 7             for i in range(tlen - 1):
 8                 bmB[ord(T[i])] = tlen - i - 1
 9         
10         def get_suff(T, suff):
11             tlen = len(T)
12             for i in range(tlen - 2 , -1 , -1):
13                 k = i
14                 while k > 0 and T[k] == T[tlen - 1 - i + k]:
15                     k -= 1
16                 suff[i] = i - k
17         
18         def get_bmG(T, bmG):
19             tlen = len(T)
20             suff = [0] * (tlen + 1)
21             get_suff(T, suff)
22             for i in range(tlen):
23                 bmG[i] = tlen
24             for i in range(tlen - 1, -1, -1):
25                 if suff[i] == i + 1:
26                     for j in range(tlen - 1):
27                         if bmG[j] == tlen:
28                             bmG[j] = tlen - 1 - i
29             for i in range(tlen - 1):
30                 bmG[tlen - 1 - suff[i]] = tlen - 1 - i
31         
32         i = 0
33         tlen = len(needle)
34         slen = len(haystack)
35         bmG = [0] * tlen
36         bmB = []
37         get_bmB(needle, bmB)
38         get_bmG(needle, bmG)
39         
40         while i <= slen - tlen:
41             j = tlen - 1
42             while j > -1 and haystack[i + j] == needle[j]:
43                     j -= 1
44             if j == -1:
45                 return i
46             
47             i += max(bmG[j], bmB[ord(haystack[i+j])] - (tlen - 1 - j))
48         
49         return -1
50 
51 # https://leetcode-cn.com/problems/implement-strstr/solution/c5chong-jie-fa-ku-han-shu-bfkmpbmsunday-by-2227

字符串匹配的Boyer-Moore算法

 1 class Solution:
 2     def strStr(self, haystack: str, needle: str) -> int:
 3         if needle == '': return 0
 4         
 5         slen = len(haystack)
 6         tlen = len(needle)
 7         if slen < tlen: return -1
 8         
 9         i = j = 0
10         m = tlen
11         
12         while i < slen:
13             if haystack[i] != needle[j]:
14                 for k in range(tlen - 1, -1, -1):
15                     if m < slen and needle[k] == haystack[m]:
16                         break
17                 i = m - k
18                 j = 0
19                 m = i + tlen
20                 if m > slen:
21                     return -1
22             else:
23                 if j == tlen - 1:
24                     return i - j
25                 i += 1
26                 j += 1
27         
28         return -1
29 
30 # https://leetcode-cn.com/problems/implement-strstr/solution/c5chong-jie-fa-ku-han-shu-bfkmpbmsunday-by-2227

 

转载于:https://www.cnblogs.com/catyuang/p/11125984.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值