个人思路总结:
可以遍历一次,找到第一个相等的就可以返回。可以利用c++自带的substr()函数。
代码如下:
class Solution {
public:
int strStr(string haystack, string needle) {
int len_needle = needle.size();
int len_haystack = haystack.size();
int index = -1;
if(len_needle==0)
return 0;
for(int i=0;i<=len_haystack-len_needle;i++)
{
if(haystack.substr(i,len_needle)==needle)
{
index = i;
break;
}
}
return index;
}
};