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 -- 使用JunitReport导出测试报告

    使用Robotium进行测试的时候,要想可以导出明了的测试结果,可以使用junitreport来实现 junit-report下载地址:https://github.com/jsankey/andro ...

  2. CVE-2014-0196(马拉松赛跑bug)

    /* * CVE-2014-0196: Linux kernel <= v3.15-rc4: raw mode PTY local echo race * condition * * Sligh ...

  3. WIN32读写INI文件方法

      在程序中经常要用到设置或者其他少量数据的存盘,以便程序在下一次执行的时候可以使用,比如说保存本次程序执行时窗口的位置.大小.一些用户设置的 数据等等,在 Dos 下编程的时候,我们一般自己产生一个 ...

  4. [转] boost::any的用法、优点和缺点以及源代码分析

    boost::any用法示例: #include <iostream> #include <list> #include <boost/any.hpp> typed ...

  5. Linux最大文件打开数

    介绍 在Linux下有时会遇到Socket/File : Can't open so many files的问题.其实Linux是有文件句柄限制的,而且Linux默认一般都是1024(阿里云主机默认是 ...

  6. ProGuard 代码混淆

    简介 Java代码是非常容易反编译的.为了很好的保护Java源代码,我们往往会对编译好的class文件进行混淆处理. ProGuard是一个混淆代码的开源项目.它的主要作用就是混淆,当然它还能对字节码 ...

  7. 对相同id的input框的循环判断

    $("input[id=sl]").each(function(){ alert(10); });

  8. ImageIO.wtrie生成jpg图片质量损失方案:BufferedImage生成jpg图片文件流

    Iterator<ImageWriter> iterator = ImageIO.getImageWritersByFormatName("jpeg"); ImageW ...

  9. java语言基础特性

    使用Java5中的类型安全枚举和注解(以及元注解)编程 http://www.importnew.com/11053.html

  10. C#获取磁盘列表与信息

    方法1:使用Environment //获取当前计算机逻辑磁盘名称列表 String[] drives = Environment.GetLogicalDrives(); Console.WriteL ...