暴力匹配,时间复杂度为O(n*n),建议KMP
class Solution:
# @param {string} haystack
# @param {string} needle
# @return {integer}
def strStr(self, haystack, needle):
if len(haystack) < len(needle): return -1
i = 0
while i < len(haystack)-len(needle)+1:
j=i;k=0
while k < len(needle):
if haystack[j] == needle[k]: j+=1; k+=1
else: break
if k == len(needle): break
else:i+=1
if i==(len(haystack)-len(needle)+1): return -1
else: return i