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. [Angular 2] Managing State in RxJS with StartWith and Scan

    The scan operator in RxJS is the main key to managing values and states in your stream. Scan behaves ...

  2. [AngularJS] New in Angular 1.5 ng-animate-swap

    <!DOCTYPE html> <html ng-app="MyApplication"> <head> <link rel=" ...

  3. Android 自定义View (一)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24252901 很多的Android入门程序猿来说对于Android自定义View ...

  4. 设计模式之—简单工厂模式<Simple Factory Pattern >

    简单工厂模式结构图: 简单工厂模式以简单的加减乘除运算为例: 运算符类(Operation): namespace CalcTest.Simple_Factory_patterns { class O ...

  5. linux的comm命令

    http://blog.csdn.net/apache6/article/details/5789669

  6. PHP Directory 函数

    PHP 5 Directory 函数 函数 描述 chdir() 改变当前的目录. chroot() 改变根目录. closedir() 关闭目录句柄. dir() 返回 Directory 类的实例 ...

  7. JAVA打印类(带预览)

    package tool; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; ...

  8. 武汉科技大学ACM:1008: 明明的随机数

    Problem Description 明明想在学校中请一些同学一起做一项问卷 调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个, ...

  9. 剖析c++(三) 类对象在内存中的布局

    基本规则: 1.各data member的自然边界为其字节大小(char为1,short为2,int为4),并按照各自的自然边界对齐: 2.整个object的总自然边界为最大data memeber的 ...

  10. QT5-控件-QTimeEdit和QTime

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTimeEdit> #i ...