class Solution:
def strStr(self, haystack: str, needle: str) -> int:
# 判斷needle是否為NOne或者為空字串
if not needle or len(needle) == 0:
return 0
# 定義兩個變數,用來接收needle的長度
length,index = len(needle),0
# 進行回圈,當index的值
while index <= len(haystack) - length:
# 判斷是否可以匹配
if haystack[index : index + length] == needle:
return index
# 索引加一
else:
index += 1
# 代表匹配失敗,回傳-1
return -1
A = Solution()
print(A.strStr("hello","ll"))
print(A.strStr("helll","hahah"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/96408.html
標籤:Python
上一篇:Python3常用知識庫鏈接
下一篇:python 實作漢諾塔
