本文主要是介绍28. Implement strStr()【E】【59】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Subscribe to see which companies asked this question
注释掉的是流氓版本
实现的就是两个指针,进行比较,O(l1*l2)
class Solution(object):def strStr(self, haystack, needle):s1 = haystacks2 = needlel1 = len(s1)l2 = len(s2)if s2 == '':return 0if l1 < l2:return -1if s1 == s2:return 0for i in xrange(l1 - l2 + 1):flag = 0for j in xrange(l2):#print s1[i + j],s2[j]if s1[i+j] != s2[j]:flag = 1breakif flag == 0:return i#print ''return -1'''try:return s1.index(s2)except:return -1'''
这篇关于28. Implement strStr()【E】【59】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!