题目:

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. java创建对象的5种方法

    java是面向对象的,所以在使用中经常会去创建对象,而我们一般创建对象只会使用new关键字去创建,这里给大家总结一下在java中创建对象的5中方法: 使用new关键字 } → 调用了构造函数 使用Cl ...

  2. Spring基础03——Spring IOC和DI概述

    1.什么是IOC与DI IOC(Inversion of Control):其思想是反转资源获取方向,传统的资源查找方式要求组件想容器发起请求查找资源,作为回应,容器适时的返回资源,而应用了IOC之后 ...

  3. HMP许可更新

    1.打开HMP License Manager,显示路径(License File Name)下的文件为最新许可,点击Activate License后,点击Show License Details, ...

  4. The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team (线段树)

    题目链接:https://nanti.jisuanke.com/t/41387 题目大意:对于给定序列,求出对于每个位置求出比该数大于m的最靠右的位置. 思路:首先对序列进行离散化,然后对于每个数的下 ...

  5. mac+react-native环境搭建

    主要参考 https://reactnative.cn/docs/getting-started.html react-native中文网 IOS版 1.Node v10以上.Watchman 和 R ...

  6. 给虚拟机CentOS7扩容(lvm方式)

    虚拟机中centos7原有容量不够了,需要进行扩容. 可以使用图形工具gparted来进行操作,安装和使用可自行百度.但需要注意的是,这篇文章提到:一定要用parted中的mkfs命令格式化分区,用系 ...

  7. HTML5的新特性:范围样式,又叫做<style scoped>

    Chromium 最近实现了一个HTML5的新特性:范围样式,又叫做<style scoped> .开发者可以通过为根元素设定一个添加了scoped属性的style标签,来限制样式只作用于 ...

  8. 错误: 找不到或无法加载主类 org.sang.BlogserverApplication

    错误: 找不到或无法加载主类 org.sang.BlogserverApplication

  9. Java——常用类(File)

    [File]    <1>java.io.File类代表系统文件名(路径和文件名).         ----注意:这里代表的只是文件名,而不是物理上的文件(硬盘上的数据),通过该类无法读 ...

  10. android 开发,视频群聊引发短信异常

    说到 NDK 开发,其实是为了有些时候为了项目需求需要调用底层的一些 C/C++ 的一些东西:另外就是为了效率更加高些. 但是很多时候能不用就不用:这个是啥原因?个人感觉有些时候是觉得麻烦,首先要配置 ...