Implement strStr().

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

问题:实现 strStr() 函数。即在  haystack 中匹配 needle 字符串。

可以理解为,实际上这道题是在问如何实现 KMP(Knuth–Morris–Pratt) 算法。这是个效率比较高的算法,只需要扫一遍 haystack 就可以得到结果,耗时 O(n) 。在扫之前需要做的是对 needle 字符串预处理,得到 needle 各个前缀字符串[0...i]中 既是真实前缀又是真实后缀的子字符串的最长长度。理解这句话,需要注意的是“既是真实前缀又是真实后缀” 是指在某一 needle前缀而言的。

需要详细理解 KMP 算法,可以参考前一篇文章我所理解的 KMP(Knuth–Morris–Pratt) 算法,或许有帮助。

 vector<int> LofPS;

 /**
* 求 s 中所有前缀字符串[0...i]各自的 既是真实前缀又是真实后缀的子字符串最长长度,存于 LofPS[i]。
*
* 例如令 len = LofPS[i],则表示 真实前缀s[0...len-1] 和 真实后缀s[ i-len+1...i ] 相等。
*
*/
vector<int> computePrefixSuffix(string s){ // LofPS[i] 表示 s[0....i] 部分中,既是真实前缀又是真实后缀的子字符串最长长度。
vector<int> LofPS(s.size()); if (s.size() == ) {
return LofPS;
} LofPS[] = ; int len = ;
int i = ; while (i < s.size()) { if (s[i] == s[len]) {
len++;
LofPS[i] = len;
i++;
continue;
} if (len != ) {
// 利用之前计算的结果。这里是一个需要理解的点。
// 根据已计算的 LofPS[len-1]部分 真实前缀、真实后缀的相等的最长长度,定位同样匹配 s 前缀但是更短的子字符串。
len = LofPS[len - ];
}else{
LofPS[i] = ;
i++;
}
} return LofPS;
} int strStr(string haystack, string needle) { // 计算 needle 中所有前缀字符串[0...idx]各自的真实前缀且是真实后缀的最长长度。
vector<int> tmp(needle.size());
LofPS = tmp; LofPS = computePrefixSuffix(needle); int i = ;
int k = ; while (i < haystack.size() && k < needle.size()) {
if (haystack[i] == needle[k]) {
i++;
k++;
continue;
} if (LofPS[k-] != ) {
k = LofPS[k-];
continue;
} if (haystack[i] == needle[]) {
k = ;
i++;
}else{
k = ;
i++;
}
} if (k == needle.size()) {
return i - k;
}else{
return -;
}
}

[LeetCode] 28. Implement strStr() 解题思路的更多相关文章

  1. 44. leetcode 28. Implement strStr()

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

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

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

  3. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

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

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

  5. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  6. Leetcode 28——Implement strStr()

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

  7. [leetcode]28. Implement strStr()实现strStr()

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

  8. [LeetCode] 28. Implement strStr() ☆

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

  9. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

随机推荐

  1. 【转】IOS7 MPMoviePlayerViewController横屏显示

    在应用程序中用到MPMoviePlayerViewController时,有时需要保持应用程序为竖屏状态,而视频播放器显示为横屏,如何做呢?如果采用强制横屏的方法,应用审核的时候是不会通过的,因为该方 ...

  2. 使用JDK自带缓存(Cache)实现Cookie自动登陆

    自定义一个缓存类AdminCache package jw.admin.common; import jw.base.entity.Admin; import sun.security.util.Ca ...

  3. 用Hexo搭建属于自己的Blog

    什么是Hexo 简单的来说,Hexo是一款基于Node.JS的静态博客框架,官方给它的描述是"A fast, simple & powerful blog framework&quo ...

  4. JavaScript中几个可以转化为false的值

    1.[0,NaN,“”,null,undefined]都可以直接转化为false,但这几个值不是完全相等的 var arr = [0,"",false,null,undefined ...

  5. 原生js-拉勾网首页效果

    拉勾网首页公司广告位的悬浮划过效果着实很吸引我.如下(不会做动图!--,感兴趣的可以去拉勾看看): 此处最吸引我的地方在于将鼠标划过上面一排公司列表时,感觉像是绿色的区块跟着你的鼠标移动一样,颇有动感 ...

  6. mysql怎么限制某些查询语句的执行?

    mysql怎么限制某些查询语句的执行? 比如某些sql语句执行时间很长,超过10s,怎么样超过10s就不让其执行? 后续更新中...

  7. CSS中伪类的使用

    原文:http://www.cnblogs.com/guopei/archive/2011/04/16/2017627.html 何为伪类? 也就是实际实现了类的效果,但是并没有实际添加到标签中的类, ...

  8. sqlserver中的统计语法

    set statisitcs io {on | off} 显示与执行的sql语句有关的磁盘活动量的信息 set statistics profile {on | off} 显示语句的配置文件信息 se ...

  9. C/C++安全编码-字符串

    1 字符串     1.1 字符串基础 字符串提供命令行参数.环境变量.控制台输入.文本文件及网络连 接,提供外部输入方法来影响程序的行为和输出,这也是程序容易出错的地方.字符串是一个概念,并不是C/ ...

  10. Supervisor的一些基础使用

    Supervisor是一个进程监控程序. 满足的需求是:我现在有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断.当进程中断的时候我希望能自动重新启动它,此时,我就需要使用到了 ...