Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1 暴力解法:如果模式串匹配失败,需要回溯到模式串的起始位置 我们想可以不用回溯到起始位置,如图:
如果能确定A==B 可以直接跳到C跟d比较 问题就转化成了如何求模式串中前缀串于后缀串相等的K个





 class Solution:
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if needle =='':
return 0
nexts=self.caclNext(needle)
ans = -1
i = 0
j = 0
while i < len(haystack):
if(j==-1 or haystack[i] == needle[j]):
i += 1
j += 1
else:
j = nexts[j]
if j == len(needle):
ans = i - len(needle)
break
return ans def caclNext(self, p):
nexts = [0]*(len(p))
nexts[0] = -1
k = -1
j = 0
while j < len(p) - 1:
if k == -1 or p[j] == p[k]:
k += 1
j += 1
nexts[j] = k
else:
k = nexts[k]
return nexts

28. Implement strStr()(KMP字符串匹配算法)的更多相关文章

  1. 28.Implement strStr()---kmp

    题目链接:https://leetcode.com/problems/implement-strstr/description/ 题目大意:字符串匹配,从字符串中,找到给定字符串第一次出现的位置下标, ...

  2. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  3. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  4. BM和KMP字符串匹配算法学习

    BM和KMP字符串匹配算法学习 分类: 研究与学习 字符串匹配BM(Boyer-Moore)算法学习心得 http://www.cnblogs.com/a180285/archive/2011/12/ ...

  5. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...

  6. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

  7. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

  8. [LeetCode] 28. Implement strStr() 解题思路

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

  9. C# 写 LeetCode easy #28 Implement strStr()

    28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...

随机推荐

  1. Batch Normailzation

    转自:http://blog.csdn.net/malefactor/article/details/51476961

  2. 重载(Overload)

    重载(Overload) 重载(overloading) 是在一个类里面,方法名字相同,而参数不同.返回类型可以相同也可以不同. 每个重载的方法(或者构造函数)都必须有一个独一无二的参数类型列表. 最 ...

  3. ICO图标的制作与应用

    制作参看:http://www.shouce.ren/tool/ico?action=make 示例: <link href="./js/favicon.ico" rel=& ...

  4. Hadoop1的安装

    目前hadoop1的稳定版本是1.2.1,我们以版本1.2.1为例详细的介绍hadoop1的安装,此过程包括OS安装与配置,JDK的安装,用户和组的配置,这些过程在hadoop2也有可能用到. Had ...

  5. 4190. Prime Palindromes 一亿以内的质数回文数

    Description The number 151 is a prime palindrome because it is both a prime number and a palindrome ...

  6. VC++ 给你的代码强制加一个硬断点

    类似与Javascript的 debugger; Hard code a debugger breakpoint If you need to insert a hard breakpoint in ...

  7. JavaScript格式化日期输出

     JavaScript Code  12345678910111213141516171819202122232425262728   <script>     window.onload ...

  8. shell基础(二)

    echo命令 Shell 的 echo 指令是用于字符串的输出. #!/bin/sh read name #读取标准输入的行 echo "$name It is a test" e ...

  9. CGContextRef用法

    本文转载至 http://blog.csdn.net/perfect_promise/article/details/7660220 quartz 是主要的描画接口,支持基于路径的描画. 抗锯齿渲染. ...

  10. HTML-CSS文件链接HTML的三种方式

    <!--css文本的链接方式有三种:分别是内联定义.链入内部css.和链入外部css--> <!--1.代码为:--> <!--<html> <head ...