题意理解:
自己实现strStr()函数;
题目分析:
穷举,注意边界处理;
解题代码:
class Solution {
public:
int strStr(string haystack, string needle) {
int sizeofHayStack=haystack.length();
int sizeofNeedle=needle.length();
if(sizeofNeedle>sizeofHayStack){
return -1;
}
int index=0;
for(int i=0; i<sizeofHayStack; i++){
index=i;
//cout<<"index= "<<index<<endl;
for(int j=0; j<sizeofNeedle; j++){
if(sizeofNeedle>sizeofHayStack-i){
return -1;
}
//cout<<"needle[j]= "<<needle[j]<<endl;
//cout<<"haystack[i+j]= "<<haystack[i+j]<<endl;
if(needle[j]!=haystack[i+j]){
index=-1;
break;
}
}
if(index!=-1){
return index;
}
}
return index;
}
};