您的位置:首页 > 其它

leetcode 字符串匹配Implement strStr()

2015-08-12 13:47 423 查看
mplement strStr().

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

判断字符串needle是否在字符串haystack中,若在在返回第一次出现的下标。

class Solution {
public:
int strStr(string haystack, string needle) {
int l=needle.length();
int l1=haystack.length();
int i;
for(i=0;i<l1-l+1;i++)
{	string haystack1;
haystack1.assign(haystack.substr(i,l));
cout<<haystack1<<endl;
if(haystack1.compare(needle)==0)return i;
}
return -1;}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: