#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. 【最大流】【HDU2883】【kebab】

    题意: 有一个烧烤机,每次最多能烤 m 块肉,现在有 n 个人来买烤肉,每个人到达时间为 si,离开时间为 ei,点的烤肉数量为 ci,点的烤肉所需烘烤时间为 di, 每个人要烤的肉可以分成若干份在同 ...

  2. inline函数

    C语言中的inline函数并不是单纯的用函数块内容来替换,也可能存在局部变量啥的.另外,使用inline定义的函数只是建议编译器作为内联函数处理,但并不一定真会这样处理.inline一般直接在头文件中 ...

  3. Broadcast详解

    今天闲来无事,研究了下Android的Broadcast,发现Broadcast在Android系统中担任着很艰巨的角色. Broadcast是Android的四大组件之一:Broadcast分为普通 ...

  4. Oracle 添加 scott 示例用户

    学习SQL有一段时间了,但是也忘记的差不多了,今天有赶紧复习复习,然后发现一个问题,为啥之前看的视频教程,马士兵用的Oracle有scott用户和那些表格,而我的没有?难道是Oracle取消了?然后百 ...

  5. EOFError:EOF when reading a line

    Sublime2编译Python程序EOFError:EOF when reading a line: 是因为Sublime2对于python中运行含有input或者raw_input的python代 ...

  6. 工作中的第一份LoadRunner脚本

    录制的第一份脚本 虽然是第一份但是调试执行,跑场景等都成功了. Action() { web_url("login1.jsp", "URL=http://192.168. ...

  7. Android01-概述

    1.Android特点    开源和免费    强大的研发力量,完整的生态圈    互联网服务的支持 2.Android系统架构    应用层    应用框架层    系统运行库层    Linux内 ...

  8. python map, reduce,filter 使用

    参考python built-on function: http://docs.python.org/2.7/library/functions.html?highlight=map%20reduce ...

  9. C语言的本质(6)——位运算

    位运算是指按二进制进行的运算.在系统软件中,常常需要处理二进制位的问题.C语言提供了6个位操作运算符.这些运算符只能用于整型操作数,即只能用于带符号或无符号的char,short,int与long类型 ...

  10. POJ 1182 食物链(并查集拆点)

    [题目链接] http://poj.org/problem?id=1182 [题目大意] 草原上有三种物种,分别为A,B,C A吃B,B吃C,C吃A. 1 x y表示x和y是同类,2 x y表示x吃y ...