数据结构算法操作试题(C++/Python):数据结构算法操作试题(C++/Python)——目录
1. 题目
leetcode 链接:https://leetcode-cn.com/problems/implement-strstr/submissions/
2. 解答
python: 36ms, 10.8MB
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not haystack and not needle: return 0
if haystack == needle: return 0
lenNeedle = len(needle)
for i in range(len(haystack) - lenNeedle + 1):
if haystack[i : i + lenNeedle] == needle:return i
return -1
其他方法看 leetcode 链接 评论区~