leetcode28 Implement strStr()

 1 """
 2 Implement strStr().
 3 Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
 4 Example 1:
 5 Input: haystack = "hello", needle = "ll"
 6 Output: 2
 7 Example 2:
 8 Input: haystack = "aaaaa", needle = "bba"
 9 Output: -1
10 """
11 """
12 用切片的方法匹配字符
13 """
14 class Solution:
15     def strStr(self, haystack, needle):
16         for i in range(len(haystack) - len(needle)+1):#bug此处不加1,input如果为两个空字符串,无法输出i
17             if needle == haystack[i:i+len(needle)]:
18                 return i
19         return -1
原文地址:https://www.cnblogs.com/yawenw/p/12337722.html