题目:

class Solution {
public:
int strStr(string haystack, string needle) {
if(needle.empty()){
return 0;
} vector<int> next;
int i = 0;
int j = 0;
int sLen = haystack.size();
int tLen = needle.size(); next = getNext(needle); while((i<sLen)&&(j<tLen)){
if((j==-1)||(haystack[i]==needle[j])){
i++;
j++;
}
else{
j = next[j];
}
} if(j==tLen){
return i-j;
} return -1;
} vector<int> getNext(string pattern){
vector<int> next;
int j = -1;
int i = 0;
int len = pattern.size(); next.push_back(-1); while(i<len-1){
if(j==-1||pattern[i] == pattern[j]){
i++;
j++;
next.push_back(j);
}
else{
j = next[j];
}
}
return next;
}
};

思路:KMP算法,重点在getNext。KMP算法的话打算另外写,这里就不写了。

上面这个是未经过优化的,如果进行优化则:

    vector<int> getNext(string pattern){
vector<int> next;
int j = -1;
int i = 0;
int len = pattern.size(); next.push_back(-1); while(i<len-1){
if(j==-1||pattern[i] == pattern[j]){
i++;
j++;
if(pattern[j]!=pattern[i]){
next.push_back(j);
}
else{
next.push_back(next[j]);
}
}
else{
j = next[j];
}
}
return next;
}

就是当前缀和后缀相同的时候那么应该应该继续递归,因为相同字符没意义。

值得注意的是while((i<sLen)&&(j<tLen)),一定要先将needle.size()haystack.size()赋值给新的变量保存,不然会出错:

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() 实现strStr()函数

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

  3. Leetcode #28. Implement strStr()

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

  4. Java [leetcode 28]Implement strStr()

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

  5. [LeetCode] 28. Implement strStr() 解题思路

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

  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. 关于jsp删除成功,添加成功等之后 页面自动跳转的js写法

    因为比较常用,所以写在博客里保存起来,防止以后忘了不会写了: 删除成功,<span id="time" style="background:red"> ...

  2. redux中间件来执行异步操作

    在redux中我们都是执行同步操作,如果我们想要执行异步操作,那么我们就需要依赖到中间件,具体的中间件的概念我就不描述了相信官方文档更详尽.现在就描述下具体的用法,就已我们项目中用到的最多的数据请求为 ...

  3. 最佳实践:深度学习用于自然语言处理(Deep Learning for NLP Best Practices) - 阅读笔记

    https://www.wxnmh.com/thread-1528249.htm https://www.wxnmh.com/thread-1528251.htm https://www.wxnmh. ...

  4. vue请求数据

    vue-resource: 推荐教程:https://www.runoob.com/vue2/vuejs-ajax.html 1. 需要安装vue-resource模块, 注意加上 --save np ...

  5. 【SaltStack官方版】—— job management

    JOB MANAGEMENT New in version 0.9.7. Since Salt executes jobs running on many systems, Salt needs to ...

  6. awk基础学习

    2019-12-20 需要巧记,很多格式,学习难度:grep.sed.awk awk知识概述 1三剑客awk命令介绍2三剑客awk命令执行原理语法结构3三剑客awk命令实操练习查询替换信息排除(取反) ...

  7. 向上取整&向下取整

    使用floor函数. floor(x)返回的是小于或等于x的最大整数.eg.      floor(1.5) = 1 floor(-2.5) = -3 使用ceil函数. ceil(x)返回的是大于x ...

  8. 【BZOJ1521】Est(单调队列优化DP)

    题意:From https://www.cnblogs.com/CXCXCXC/p/4725249.html 思路:本身就两维状态了,把问题关键s[i][j]写成二维比一维好写多了 #include& ...

  9. [BZOJ4205][FJ2015集训]卡牌配对

    题目:卡牌配对 传送门:None 题目大意:有$n_1$张$X$类牌和$n_2$张$Y$类类牌,每张卡牌上有三个属性值:$A,B,C$.两张卡牌能够配对,当且仅当,存在至多一项属性值使得两张卡牌该项属 ...

  10. [CSP-S模拟测试]:位运算(数学)

    题目传送门(内部题72) 输入格式 输入文件$bit.in$ 每个输入文件包含$T$组测试数据.输入文件的第一行为一个整数$T$,表示数据组数.接下来$T$行,每行表示一组测试数据每组测试数据包括三个 ...