日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

leetcode28. 实现strStr()

發布時間:2024/4/15 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的leetcode28. 实现strStr()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。