您的位置:首页 > 其它

Implement strStr

2015-10-16 21:27 267 查看
mplement strStr().

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

先来暴力解法: (kmp看懂后再补。。)

class Solution
{
public:
int strStr(string haystack, string needle)
{
if (needle == "")
return 0;
int i = 0;
int j = 0;
while (i < haystack.size() && j < needle.size())
{
if (haystack[i] == needle[j])
{
i++;
j++;
}
else
{
i = i - j + 1;
j = 0;
}
}

if (j == needle.size())
return i - j;
else
return -1;

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: