#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的更多相关文章

  1. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

  3. LeetCode Implement strStr()(Sunday算法)

    LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...

  4. [LeetCode] Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  5. LeetCode: Implement strStr() [027]

    [题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...

  6. leetcode——Implement strStr() 实现字符串匹配函数(AC)

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  7. LeetCode Implement strStr() 实现strstr()

    如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...

  8. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  9. 【LeetCode】28. Implement strStr() 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...

随机推荐

  1. JAVA NIO 真正做到处理一个事件

    如下图所示: 组1:如果只有上面的红框,不能真正处理该事件,下次执行select()方法,仍然可以select出来该事件.出现死循环现象.如果只有下面的红框,下次select()结果为0,如果外层循环 ...

  2. C#整理6——数组的应用

    数组的应用:(一).冒泡排序.1.冒泡排序是用双层循环解决.外层循环的是趟数,里层循环的是次数.2.趟数=n-1:次数=n-趟数.3.里层循环使用if比较相临的两个数的大小,进行数值交换. 作业:1. ...

  3. button属性值

    AccessibilityObject 取得指定給控制項的 AccessibleObject. (繼承自 Control). AccessibleDefaultActionDescription 取得 ...

  4. javascript中关于数组的迭代方法

    //都接受3个参数,分别为:值.在数组中的位置.数组对象本身 var num = [2, 1, 5, 4, 2, 1, 6, 8, 19]; //every:若每一项都返回true,则返回true v ...

  5. Iframe和父窗口互调方法的集合

    1.子iframe里调用父级的方法:window.parent.document.   2.父级里调用子集iframe:window.frames["iframe_text"].d ...

  6. SqlServer2008(R2) 数据库使用外网IP实例连接服务器

    1.打开sql2008,使用windows身份登录 2.登录后,右键选择"属性".左侧选择"安全性",选中右侧的"SQL Server 和 Windo ...

  7. WCF Test Client

    WCF测试客户端(WCF Test Client)是一个用来测试WCF服务程序的调试工具,能够使开发WCF服务更加方便. 在Visual Studio之外打开WCF测试客户端有两种方法:第一种方法是到 ...

  8. Spring事务传播机制详解

    1 事务的传播属性(Propagation) 1) REQUIRED ,这个是默认的属性 Support a current transaction, create a new one if none ...

  9. tnsping慢的问题解决

    1.检查网络ping主机或IP是否正常,DNS是否设置正确 2. 检查防火墙设置 3.检查listener.log日志,查看是否有大量连接连入. 4.检查listener.log日志文件是否过大,如果 ...

  10. _cdel stdcall

    __cdecl 是C Declaration的缩写(declaration,声明),表示C语言默认的函数调用方法:所有参数从右到左依次入栈,这些参数由调用者清除,称为手动清栈.被调用函数不会要求调用者 ...