[leetcode] 21. Implement strStr()
这个题目是典型的KMP算法,当然也可以试试BM,当然有关KMP和BM的介绍阮一峰曾经写过比较好的科普,然后july也有讲解,不过那个太长了。
先放题目吧:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a
char *orString, please click the reload button to reset your code definition.
就是找到haystack内是否有needle这个字符串,字符串查找算法。
KMP主要是建立一个next的部分匹配数组,来用空间换时间。next的生成函数是KMP关键,解法如下:
class Solution {
public:
vector<int> generateNext(char *str)
{
int len = strlen(str);
vector<int> tmp;
tmp.push_back(0);
for (int i = 1, j = 0; i < len; i++)
{
while (j > 0 && str[i] != str[j])
{
j = tmp.at(j - 1);
}
if (str[i] == str[j])
{
j++;
}
tmp.push_back(j);
}
return tmp;
}
int strStr(char *haystack, char *needle)
{
vector<int> next = generateNext(needle);
int lenH = strlen(haystack);
int lenN = strlen(needle);
if (lenN == 0)
{
return 0;
}
if (lenH < lenN)
{
return -1;
}
int i, j;
for (i = 0, j = 0; i < lenH; i++)
{
while (j > 0 && haystack[i] != needle[j])
{
j = next[j - 1];
}
if (haystack[i] == needle[j])
{
j++;
}
if (j == lenN)
{
return i - lenN + 1;
}
}
return -1;
}
};
[leetcode] 21. Implement strStr()的更多相关文章
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- 【leetcode】Implement strStr() (easy)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【leetcode】Implement strStr()
Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
随机推荐
- nfs只能挂载为nobody的解决方法
不得不承认centos6较centos5发生了很大的变化,在新部署的centos 6.4上又遇到nfs挂载的问题.问题现象是,在配置完nfs后,无论配置里指定的是何用户,挂载成功后显示的只能是nobo ...
- css3 fileter始终效果 图片渲染
http://www.w3cplus.com/css3/ten-effects-with-css3-filter
- topGO
前面我们讲过GO.db这个包,现在接着延伸topGO包,该包是用来协助GO富集分析 1)安装 if("topGO" %in% rownames(installed.packages ...
- gcd,扩展欧几里得,中国剩余定理
1.gcd: int gcd(int a,int b){ ?a:gcd(b,a%b); } 2.中国剩余定理: 题目:学生A依次给n个整数a[],学生B相应给n个正整数m[]且两两互素,老师提出问题: ...
- Animation.wrapMode循环模式
WrapMode.Default:从动画剪辑中读取循环模式(默认是Once). WrapMode.Once:当时间播放到末尾的时候停止动画的播放. WrapMode.Loop:当时间播放到末尾的时候重 ...
- SQL日期和时间函数
使用这些函数可以计算日期和时间值.例如,假设您希望了解通常在一周中哪一天的销售量最高.使用 DAYOFWEEK 函数,您可以创建一个公式来标识每天的销售订单数量.再比如,假设您希望比较在过去的一年中的 ...
- 【校招面试 之 C/C++】第11题 C++ 纯虚函数
1.纯虚函数 成员函数的形参后面写上=0,则成员函数为纯虚函数. 纯虚函数声明: virtual 函数类型 函数名 (参数表列) = 0: class Person { virtual void Di ...
- Spring集成Redis使用注解
转载:http://blog.csdn.net/u013725455/article/details/52129283 使用Maven项目,添加jar文件依赖: <project xmlns=& ...
- 网页flash播放代码
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100%" hei ...
- Vim基础教程
一.简介 世界上只有三种编辑器,EMACS.VIM和其它. 我们所处的时代是非常幸运的,有越来越多的编辑器,相对于古老的VIM和EMACS,它们被称为现代编辑器.我们来看看这两个古董有多大年纪了: * ...