共计 290 个字符,预计需要花费 1 分钟才能阅读完成。
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1
解法
class Solution:
def strStr(self, haystack: 'str', needle: 'str') -> 'int':
if len(needle)<1:
return 0
length=len(needle)
for x in range(len(haystack)-length+1):
if haystack[x:x+length]==needle:
return x
return -1
正文完
请博主喝杯咖啡吧!