题目描述:
此题有点像横向扫描,如果needle字符串在haystack里面,则返回该下标值,否则返回-1
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if needle in haystack:
return haystack.index(needle)
else:
return -1
也可以参考kmp算法:https://zhuanlan.zhihu.com/p/83334559