标签:hello += self else 变量 实现 not ret 长度
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"))
标签:hello += self else 变量 实现 not ret 长度
原文地址:https://www.cnblogs.com/cong12586/p/13253305.html