【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
手写一个效率非常低的。倒数的算法。(应该去用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
相关文章推荐
- LeetCode_Easy心得:28. Implement strStr()(C语言)
- LeetCode 28. Implement strStr暴力搜索和KMP算法
- [置顶] 【leetcode】28. Implement strStr()(Python & C++)(KMP算法)
- Leetcode 28. Implement strStr() KMP算法解题
- Leetcode 28. Implement strStr() (Easy) (cpp)
- Leetcode刷题记——28. Implement strStr(实现strStr)
- leetcode 28. Implement strStr()
- leetcode_28_ Implement strStr() (easy)
- Leetcode-28. Implement strStr()
- LeetCode 28. Implement strStr()
- leetcode 28. Implement strStr()
- leetcode 28. Implement strStr()
- Leetcode:28. Implement strStr()(JAVA)
- 【Easy】28. Implement strStr()
- *****(leetcode_string) Implement strStr() (KMP算法*****)
- 28. Implement strStr()-leetcode-java
- LeetCode-难题集之Implement strStr()与KMP算法
- [LeetCode] 28. Implement strStr() 实现strStr()函数
- LeetCode 28. Implement strStr()
- [勇者闯LeetCode] 28. Implement strStr()