# Leetcode 实现strStr()
# 找子串的问题 一遍遍历就行了 很简单
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if haystack == "" and needle == "":
return 0
elif haystack == "" and len(needle) > 0:
return -1
elif len(haystack) > 0 and needle == "":
return 0
else:
len_needle = len(needle)
len_haystack = len(haystack)
if len_haystack < len_needle:
return -1
else:
i = 0
while i <= len_haystack - 1:
if haystack[i:i+len_needle] == needle:
return i
else:
i += 1
return -1
LeetCode 实现strStr()
最新推荐文章于 2021-11-23 10:52:17 发布