原题:
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll" Output: 2Example 2:
Input: haystack = "aaaaa", needle = "bba" Output: -1
字符串匹配问题,四种算法,先从暴力匹配算法开始:
(1)暴力匹配:速度极慢,在匹配失败时会从下一位置继续匹配,浪费大量时间O(mn)
结果:
Success
Runtime: 1292 ms, faster than 15.84% of C++ online submissions for Implement strStr().
Memory Usage: 8.8 MB, less than 100.00% of C++ online submissions for Implement strStr().
代码:
class Solution {
public:
int strStr(string haystack, string needle) {
int size=haystack.size();
int size2=needle.size();
if(size2==0){return 0;}
if(size==0){return -1;}
for(int i=0;i<size;i++){
int x=0,y=i;
while(haystack[y]==needle[x]&&x<size2){
x++;
y++;
}
if(x==size2){return i;}
}
return -1;
}
};
(2)改进暴力匹配:使用哈希表,在哈希值相同的时候再暴力匹配,速度有一定提升。本来一开始打算写个hash函数,结果性能提升只有200ms不说内存占用还达到了600多mb,吓到我了,肯定是因为参数传递导致的。所以我又把hash函数内置了,然后性能有了极大提升,结果如下:
Success
Runtime: 4 ms, faster than 100.00% of C++ online submissions for Implement strStr().
Memory Usage: 8.9 MB, less than 99.61% of C++ online submissions for Implement strStr().
代码:
class Solution {
public:
int strStr(string haystack, string needle) {
int size=haystack.size();
int size2=needle.size();
int ret = 0;
for (int i=0;i<size2;i++){ret += needle[i];}
if(size2==0){return 0;}
if(size==0){return -1;}
for(int i=0;i<size;i++){
if(size-i<size2){return -1;}
int x=0,y=i;
int ret2 = 0;
for (int j=i;j<size2+i;j++){ret2 += haystack[j];}
if(ret==ret2){
while(haystack[y]==needle[x]&&x<size2){
x++;
y++;
}
if(x==size2){return i;}
}
}
return -1;
}
};
这就4ms了但是更好的算法还没用呢让我情何以堪啊,好像时间单位就是4ms这尼玛没有提升空间了啊。
(3)KMP:经典匹配算法,原理可以百度,最关键的是求next数组
结果:
Success
Runtime: 4 ms, faster than 100.00% of C++ online submissions for Implement strStr().
Memory Usage: 9.2 MB, less than 98.25% of C++ online submissions for Implement strStr().
代码:
class Solution {
public:
int strStr(string haystack, string needle) {
int size1=haystack.size();
int size2=needle.size();
if(size2==0){return 0;}
if(size1==0){return -1;}
int* next=(int*)malloc(size2*sizeof(int));
next[0] = -1;
int k = -1;
int l = 0;
while (l < size2 - 1)
{
if (k == -1 || needle[l] == needle[k])
{
++k;
++l;
next[l] = k;
}
else {
k = next[k];
}
}
int i=0,j=0;
while(i<size1&&j<size2){
if(j==-1||haystack[i]==needle[j]){
i++;
j++;
}
else{j=next[j];}
}
if(j==needle.size()){return i-j;}
else return -1;
}
};
(4)BM算法:这个算法是真的麻烦,以后再写把哈哈哈哈哈???