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. Robotium -- AndroidUI优化工具HierarchyViewer

    为什么使用HierarchyViewer 不合理的布局会使我们的应用程序UI性能变慢,HierarchyViewer能够可视化的角度直观地获得UI布局设计结构和各种属性的信息,帮助我们优化布局设计.H ...

  2. 捕android程序崩溃日志

    主要类别: package com.example.callstatus; import java.io.File; import java.io.FileOutputStream; import j ...

  3. 判断两条直线的位置关系 POJ 1269 Intersecting Lines

    两条直线可能有三种关系:1.共线     2.平行(不包括共线)    3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...

  4. 重学《C#高级编程》(对象与类型)

    昨天重看了下<C#高级编程>里面的对象与类型一章,发现自己有许多遗漏没懂的地方重新弄清楚明白了 先说说什么是对象吧,我个人的感觉是:在编程的世界里,一段程序就是一个事物的处理逻辑,而对象就 ...

  5. 开发部署一个简单的Servlet

    Servlet是一个执行在服务器端的Java Class文件,载入前必须先将Servlet程序代码编译成.class文件,然后将此class文件放在servlet Engline路径下.Servlet ...

  6. android 瀑布流(图片浏览)

    效果图: 瀑流流实现涉及的知识点 1.ScrollView滚动视图,我们这里用的是自定义ScrollView /** * Created by Spring on 2015/11/2. * 自定义Sc ...

  7. MSSQL 简单练习回顾

    这段时间,报了浦软培训的.NET,现在整理回顾下,算是个小小总结吧 为了便于操作,我没有在多个数据库间切换数据库实例,以一个总的数据库实例 test_demo为源进行的相关操作,代码的注释根据我的理解 ...

  8. iOS之使用QLPreviewController打开文件,处理txt文件出现乱码的情况

    iOS之使用QLPreviewController打开文件,处理txt文件出现乱码的情况 主要代码: - (id <QLPreviewItem>)previewController:(QL ...

  9. C/C++中的++a和a++

    代码: #include <iostream> #include <cstdio> using namespace std; int main(){ ; (++a)+=a; / ...

  10. IE9的BUG?jQuery的BUG?

    本文转载自http://big-student.iteye.com/blog/1898213 在IE9和IE10中,当对一个html的样式初始了一个很大的left或者top时,使用jQuery的off ...