[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 ...
随机推荐
- Hadoop 集群的一些问题
1.创建用户hadoop adduser hadoop passwd hadoop usermod -a -G hadoop hadoop chown -R hadoop:hadoop /data ...
- Asp.net中判断是否是指定页面请求的代码示例
//获取请求网址,非法请求,返回主页 if (Request.UrlReferrer != null) { string requstUrl = Request.UrlReferrer.Absolut ...
- fragment 事务回滚 ---动态创建fragment
import java.util.Date; import java.util.LinkedList; import com.qianfeng.gp08_day23_fragment5.fragmen ...
- linux系统挂载ISO文件
1 上传iso文件使用xftp上传系统oracleLinux7.3.iso光盘镜像到/toolsPackage 为方便使用,重命名光盘镜像文件[root@rhel64 software]# mv rh ...
- Spring框架的AOP技术(注解方式)
1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...
- 编写JavaScript 代码的5个小技巧
1.Array.includes 与条件判断 一般我们判断或用 || // condition function test(fruit) { if (fruit == "apple" ...
- 前端之css笔记2
1 属性选择器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- ajax序列化表单,再也不用通过data去一个个的传值了
jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,我们就可以选择一个或多个表单元素,也可以直接选择form将其序列化 这样,我们就可以把序列化的值传给ajax()作为 ...
- 2018.10.14 loj#6011. 「网络流 24 题」运输问题(费用流)
传送门 费用流入门题. 直接按照题意模拟. 把货物的数量当做容量建边. 然后跑一次最小费用流和最大费用流就行了. 代码: #include<bits/stdc++.h> #define N ...
- 2018.09.24 bzoj1816: [Cqoi2010]扑克牌(二分答案)
传送门 简单二分答案. 我们二分最终有k个牌堆. 这样joker被选择的张数≤min(k,m)\le min(k,m)≤min(k,m) 并且joker需要被选择的张数应该是∑i−1nmax(0,k− ...