求字符串haystack中子串needle第一次出现的位置
写了个简单粗暴的,先找出needle首字母在haystack所有出现位置,再看是否匹配
1 #28
2 #Runtime: 11 ms (Beats 95.49%)
3 #Memory: 13.2 MB (Beats 98.73%)
4
5 class Solution(object):
6 def strStr(self, haystack, needle):
7 """
8 :type haystack: str
9 :type needle: str
10 :rtype: int
11 """
12 if len(needle) == 0:
13 return 0
14 occur_lst = [i for (i, ch) in enumerate(haystack) if needle[0]]
15 for i in occur_lst:
16 if haystack[i : i + len(needle)] == needle:
17 return i
18 return -1