leetcode 实现28.strStr()
已战胜99.20%。可以借鉴一下
int strStr(string haystack, string needle) {
if (needle == "")
return 0;
int pos_r = 0, pos =-1;
for (int i = 0; i < haystack.length(); i++)
{
if (haystack[i] == needle[pos_r])
{
pos_r++;
pos = (pos == -1) ? i : pos;
}
else
{
pos_r = 0;
if (pos != -1)
{
i = pos;
pos = -1;
}
}
if (pos_r == needle.length())
{
return (i - pos_r + 1);
}
}
return -1;
}