题目:https://leetcode.com/problems/implement-strstr/description/
解决方案
class Solution:
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
l_n = len(needle)
l_h = len(haystack)
if l_n == 0:
return 0
elif l_n <= l_h:
for i in range(l_h):
if haystack[i:l_n+i] == needle:
return i
return -1
elif l_n > l_h:
return -1
震惊!学习了思考方式。。
class Solution:
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if haystack == "" and needle == "":
return 0
if needle == "":
return 0
if haystack == "" or needle == "" or len(haystack.split(needle)) == 1:
return -1
return len(haystack.split(needle)[0])