leetcode implement strStr python
2015-11-29 21:05
661 查看
#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
相关文章推荐
- python学习的链接
- python异常
- python以gzip header请求html数据时,response内容乱码无法解码的解决方案
- python学习笔记
- python module
- python函数装饰器(decorators)
- Python中元类
- python字典
- leetcode Remove Element python
- python下载文件
- python脚本语言实现hosts自动更新
- python操作符重载
- Python连接Mysql数据库遇到的一系列问题
- 如何利用Python实现UserCF
- Python多线程初步
- leetcode Remove Duplicates from Sorted Array python
- 利用python发送zabbix server的图形报表
- python 多线程编程
- 用python计算md5,sha1,crc32
- Python模块安装方法