class Solution {
public:
int strStr(string haystack, string needle) {
int len1 = haystack.size();
int len2 = needle.size();
int i = 0 ;
int index;
//当两个序列的长度都是0的情况,就是零位置匹配。
if(len1 == 0 && len2 == 0 ){
return 0;
}//当needle为0,haystack不为0则返回位置0。
if(len1 != 0 && len2 ==0){
return 0;
}
for(int i = 0 ; i < len1 -len2 + 1 ; i++ ){
for(int j = 0 ; j < len2 ; j++){
if(haystack[i+j] != needle[j]){
break;
}
else if(j == len2 -1){
return i;
}else{
continue;
}
}
}
return -1;
}
};
这个题目类似KMP但是不是KMP。