码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode implement strStr python

时间:2015-11-29 23:06:26      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

#kmp 
class
Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ if len(haystack) <= 0 and len(needle)<=0: return 0 arrNext=self.getNext(needle) i=0 j=0 intHLen=len(haystack) intNLen=len(needle) while i < intHLen and j < intNLen: if j==-1 or haystack[i] == needle[j]: i+=1 j+=1 else: j=arrNext[j] if j == intNLen: return i-j else: return -1 def getNext(self,needle): arrNext=dict() arrNext[0]=-1 k=-1 j=0 intLen=len(needle) while j < intLen: if k==-1 or needle[k] == needle[j]: k+=1 j+=1 arrNext[j]=k else: k=arrNext[k] return arrNext

 

leetcode implement strStr python

标签:

原文地址:http://www.cnblogs.com/allenhaozi/p/5005456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!