题目:

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. MySQL索引优化(索引三表优化案例)

    建表SQL phone.book表建立索引 [关联优化查询建议] 1.保证被驱动表的join字段已经被索引 被驱动表  join 后的表为被驱动表  (需要被查询) 2.left join 时,选择小 ...

  2. Spring基础13——Spring表达式语言:SpEL

    1.SpEL简介 Spring表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言.语法上类似于EL:SpEL使用#{...}作为界定符,所有在大框号中的字符都将被认为是Sp ...

  3. Spring基础11——Bean的作用域

    1.Bean的作用域种类 Spring中的bean的作用域分为四种:singleton.prototype.session.request,后两种很少使用,下面我们主要来学习前两种 2.singlet ...

  4. modesim 仿真问题

    > unisim (ERROR: Library path "d:/Xilinx/14.3/ISE_DS/ISE//vhdl/mti_se/10.1a/nt64/unisim/unis ...

  5. 牛客练习赛14 A n的约数 (数论)

    链接:https://ac.nowcoder.com/acm/contest/82/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288 ...

  6. 1130. Infix Expression (25)

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  7. 可决系数R^2和方差膨胀因子VIF

    然而很多时候,被筛选的特征在模型上线的预测效果并不理想,究其原因可能是由于特征筛选的偏差. 但还有一个显著的因素,就是选取特征之间之间可能存在高度的多重共线性,导致模型对测试集预测能力不佳. 为了在筛 ...

  8. Vue中 axios+QS 插件往后台传参

    之前用Vue+element写了一个后台管理系统,在登录时使用axios请求数据传参时无法正常的获取数据.发现原因是传递参数要将参数序列化.这里使用了qs插件: 简单来说,qs 是一个增加了一些安全性 ...

  9. Django orm(1)

    目录 一.orm查询 1.1配置测试脚本 1.1.1Django终端打印SQL语句的配置 1.2单表操作 1.2.1创建数据 1.2.2修改数据 1.2.3删除数据 1.2.4查询数据 1.2.5计数 ...

  10. CobaltStrike + Metasploit 联动使用

    本节的知识摘要: 通过 beacon内置的 socks功能将本地 Msf直接代入目标内网 借助 CobaltStrike的外部 tcp监听器通过 ssh隧道直接派生一个 meterpreter到本地 ...