LeetCode——28. Implement strStr()
题目:

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()的更多相关文章
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 28——Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [leetcode]28. Implement strStr()实现strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] 28. Implement strStr() ☆
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode 28 Implement strStr() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
随机推荐
- ln -在文件之间建立连接
总览 ln [options] source [dest] ln [options] source...directory POSIX 选项: [-f] GNU 选项(缩写): [-bdfinsvF] ...
- thinkphp5 select对象怎么转数组?
DB操作返回是数组.模型直接操作返回是对象 对象类型转换数组打开 database.php 增加或修改参数'resultset_type' => '\think\Collection',即可连贯 ...
- 在浏览器输入URL发生了什么
在我们输入google.com之后,浏览器上很快就会呈现出谷歌的页面,本文简单介绍一下从URL的输入到浏览器页面的展示,这中间发生了些什么. URL是什么URL全名叫统一资源定位符,uniform r ...
- 010-流程控制 while 与 until 语句
流程控制 while 与 until 语句 while循环是不定循环,也称作条件循环,只要条件成立,循环就一直继续.与for的固定循环不同 until只要条件不成立,循环就一直继续 #!/bin/ba ...
- 009-流程控制 for 语句
流程控制 for 语句 ##################### 语法一 ################################# #!/bin/bash do echo $i done ...
- 磁盘IO性能优化-实践
RAID卡缓存策略调整 原因详解 操作实例 I/O 调度算法 文件系统journal 磁盘挂载参数 操作实例 性能数据对比 RAID卡缓存策略调整 可以将RAID卡缓存策略由No Write Cach ...
- Revolver Maps-3D地球仪网站定制
这是个网站统计的小插件,大家可以看到那些国家,哪些城市对本网站进行了访问,很直观的一种表现方式. 这个小插件不需要你写任何代码,只需要去它官方网站定制你自己需要的代码.它的地址是:http://www ...
- AtCoder Beginner Contest 088 C Takahashi's Information
Problem Statement We have a 3×3 grid. A number ci,j is written in the square (i,j), where (i,j) deno ...
- 【学习】014 深入理解Http协议
Http协议入门 什么是http协议 http协议: 对浏览器客户端 和 服务器端 之间数据传输的格式规范 查看http协议的工具 1)使用火狐的firebug插件(右键->firebug-& ...
- 1--面试总结-js深入理解,对象,原型链,构造函数,执行上下文堆栈,执行上下文,变量对象,活动对象,作用域链,闭包,This
参考一手资料:http://dmitrysoshnikov.com/ecmascript/javascript-the-core/中文翻译版本:https://zhuanlan.zhihu.com/p ...