Runtime: 56 ms, faster than 36.86% of Python3 online submissions for Implement strStr().
不知道这样子好不,直接用的分隔符分割数据
其实在算法中是有这么一个匹配算法的,叫KMP算法,当然还有很多改进版的算法
class Solution:
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if len(needle) == 0:
return 0
s = haystack.split(needle)[0]
if s == haystack:
return -1
else:
return len(s)
参考:
python中在一个字符串中查找另一个字符串。实现一个字符串的find函数。
字符串匹配——Sunday算法
有机会写一下Sunday算法