您的位置:首页 > 其它

【28】Implement strStr()

2016-08-16 15:02 204 查看
Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

直接暴力做就好了
int strStr(string haystack, string needle) {
int len1=haystack.length();
int len2=needle.length();
for(int i=0;i<=len1-len2;i++){
bool flag=true;
for(int j=0;j<len2;j++){
if(haystack[i+j]!=needle[j]){flag=false;break;}
}
if(flag)return i;
}
return -1;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: