28. Implement strStr()(KMP字符串匹配算法)
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字符串匹配算法)的更多相关文章
- 28.Implement strStr()---kmp
题目链接:https://leetcode.com/problems/implement-strstr/description/ 题目大意:字符串匹配,从字符串中,找到给定字符串第一次出现的位置下标, ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- BM和KMP字符串匹配算法学习
BM和KMP字符串匹配算法学习 分类: 研究与学习 字符串匹配BM(Boyer-Moore)算法学习心得 http://www.cnblogs.com/a180285/archive/2011/12/ ...
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
随机推荐
- android打包library
最近在做开发时,遇到一个需求,就是要自定义一个控件,最后需要将其打包成android library库,然后供以后其他需求使用,由于以前很少打包library,所以这次特地学了下怎么打包. 首先先随便 ...
- 拉格朗日乘子法(Lagrange multiplier)和KKT条件
拉格朗日乘子法: KKT条件:
- Hibernate中:不看数据库,不看XML文件,不看查询语句,怎么样能知道表结构?
Hibernate中:不看数据库,不看XML文件,不看查询语句,怎么样能知道表结构? 解答:可以看与XML文件对应的域模型.
- 【BZOJ】3300: [USACO2011 Feb]Best Parenthesis(模拟)
http://www.lydsy.com/JudgeOnline/problem.php?id=3300 这个细节太多QAQ 只要将所有的括号'('匹配到下一个')'然后dfs即可 简单吧,,, #i ...
- LTP4J的使用BUG及解决方案
子墨子曾经曰过,LTP是个好模型! car老师oneplus还有bhan开发的LTP4J是个很好的项目,使用起来也非常方便,下面贴几个常见的错误使用引起的bug的log分析 1.现象描述:程序中断,生 ...
- java网络编程1-查询Internet地址
//经过dns查询后的结果会缓存起来,成功结果永久缓存,失败结果会缓存10s,通过下面的方法设置成功和失败的缓存时间 // 0为不缓存,-1为永不过期,其它单位为s Security.setPrope ...
- 【爱江山越野跑】ITRA积分认证流程
背景:目前在越野跑领域,高级别的赛事有很多,比如UTMB,TDG等,而想报名参与这些赛事需要一定的积分(ITRA积分), 而这些积分的获得,需要参与获得ITRA认证的赛事,赛事难度不同,获得的积分也不 ...
- [分享]JavaScript Quick Reference Card
pdf文件:https://files.cnblogs.com/files/MakeView660/JavaScript_Quick_Reference_Card.pdf
- Eclipse中的build path详解
http://blog.csdn.net/qqqqqq654/article/details/53043742
- hdu 1754 I Hate It(线段树之 单点更新+区间最值)
I Hate It Time Limit: 90 ...