双指针遍历字符串
class Solution {
public:
int strStr(string haystack, string needle) {
if(needle.length()==0)
return 0;
int has=0,need=0,wide=(haystack.length()-needle.length()+1);
while(has< wide)
{
if(haystack[has]==needle[need])
{
int len=1;
has++;need++;
for(;need<needle.length();)
{
if(haystack[has]==needle[need])
{
has++;need++;len++;
}
else
{
need=0;
has=has-len+1;
break;
}
}
if(len==needle.length())
return has-needle.length();
else
len=0;
}
else
{
has++;
}
}
return -1;
}
};