This commit is contained in:
youngyangyang04
2020-07-15 10:34:27 +08:00
parent 1e6702f6d2
commit 0069a6fa8e

View File

@ -8,17 +8,18 @@ https://leetcode-cn.com/problems/implement-strstr/
## C++代码 ## C++代码
``` ```
class Solution { class Solution {
public: public:
void preKmp(int* next, const string& s){ void preKmp(int* next, const string& s){
next[0]=-1; next[0] = -1;
int j=-1; int j = -1;
for(int i=1;i<s.size();i++){ for(int i = 1; i < s.size(); i++){
while(j>=0 && s[i]!=s[j+1]) while(j >= 0 && s[i] != s[j+1])
j = next[j]; j = next[j];
if(s[i]==s[j+1]) if(s[i] == s[j+1])
j++; j++;
next[i]=j; next[i] = j;
} }
} }
int strStr(string haystack, string needle) { int strStr(string haystack, string needle) {
@ -44,6 +45,5 @@ public:
} }
}; };
``` ```
> 笔者在先后在腾讯和百度从事技术研发多年利用工作之余重刷leetcode本文 [GitHub](https://github.com/youngyangyang04/leetcode-master )https://github.com/youngyangyang04/leetcode-master 已经收录欢迎starfork共同学习一起进步。 > 笔者在先后在腾讯和百度从事技术研发多年利用工作之余重刷leetcode本文 [GitHub](https://github.com/youngyangyang04/leetcode-master )https://github.com/youngyangyang04/leetcode-master 已经收录欢迎starfork共同学习一起进步。