您的位置:首页 > 其它

【leetcode】28. Implement strStr()(easy) KMP算法

2018-01-10 10:27 381 查看
匹配子串。

手写一个效率非常低的。倒数的算法。(应该去用kmp算法求解)

// 28. Implement strStr()
int strStr(string haystack, string needle) {
if (haystack.size() < needle.size())
return -1;
if (needle.size() == 0)
return 0;

for (int i = 0; i < haystack.size(); ++i)
{
for (int j = 0; j < needle.size(); ++j)
{
if (haystack[i + j] == needle[j])
{
if (j == needle.size() - 1)
return i;
}
else
{
break;
}
}
}
return -1;
}

下面复习一下kmp算法: https://www.cnblogs.com/zhangtianq/p/5839909.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: