自己写的一个一个字符匹配的while循环是什么辣鸡玩意,根本就过不了一些特殊测试集,哎,看官解吧。
class Solution:
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not needle:
return 0
elif not needle in haystack:
return -1
else:
for i in range(len(haystack)):
if haystack[i:i+len(needle)] == needle:
return i
#方法2
return haystack.find(needle)