class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
lengthh = len(haystack)
lengthn = len(needle)
if lengthn == 0:
return 0
if lengthh < lengthn:
return -1
for i in xrange(lengthh - lengthn + 1):
j = 0
while j < lengthn:
if haystack[i+j] != needle[j]:
break
else:
j += 1
if j == lengthn:
return i
return -1
简化
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
for i in range(len(haystack) - len(needle)+1):
if haystack[i:i+len(needle)] == needle:
return i
return -1