【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列
(一)题目
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
(二)解题
第一种解法:朴素匹配算法
/*
两个指针,分别指向两个字符串的首字符
如果相等则一起向后移动,如果不同i取第一个相同字符的下一个开始继续匹配
如果最后j等于needle的长度则匹配成功,返回i-j
否则返回0
*/
class Solution {
public:
int strStr(string haystack, string needle) {
int j,i;
for(i = 0 , j =0 ; i<haystack.length() && j < needle.length() ;)
{
if(i+needle.length()>haystack.length()) return -1;
if(haystack[i]==needle[j]){//如果匹配上就继续向后匹配
i++;
j++;
}
else{
i-=j-1;//回溯到匹配开始时needle的首字符对应的下一位
j=0;//j回溯到needle的首字符
}
}
if(j==needle.length()) return i-j;
else return -1;
}
};
第二种解法:KMP模式匹配算法
关于kmp,请自行百度或者大话数据结构P143页
class Solution {
public:
int strStr(string haystack, string needle) {
int hlen = haystack.length();
int nlen = needle.length();
if(hlen==0) return nlen==0?0:-1;//临界值判断
if(nlen==0) return 0;//needle为NULL,就直接返回0
int* next = new int[nlen+1];
getNext(needle,next);
int i = 0;
int j = 0;
while(i<hlen&&j<nlen){
if(j==-1 || haystack[i]==needle[j]){
i++;j++;
}
else j=next[j];
}
if(j==nlen) return i-j;//等于nlen代表匹配成功,返回i-j即needle首字符在haystack中的位置
else return -1;
}
void getNext(string& needle,int next[])
{
int i = 0;
int j = -1;
next[0] = -1;
while(i<needle.length()){
if(j==-1 || needle[i]==needle[j]){
i++;
j++;
if(needle[i] == needle[j]) next[i] = next[j];//kmp优化,防止aaaaab和aaaac前四位的无效
else next[i] = j;
}
else
j=next[j];
}
}
};
【一天一道LeetCode】#28. Implement strStr()的更多相关文章
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- 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 ...
- Leetcode 28——Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [leetcode]28. Implement strStr()实现strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] 28. Implement strStr() ☆
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode 28 Implement strStr() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
- LeetCode——28. Implement strStr()
题目: class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()){ return ...
随机推荐
- Dynamics CRM 查找字段下拉的最多10个选项的排序规则
原文链接来自DTCCh论坛http://dynamics.ms-talent.com.cn/bbs/content/?id=1406&catogory=CRM 如果你是从事dynamics c ...
- Activity的四种启动模式任务栈图解
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 今天带来另一篇Activity的文章--Activity的四种启动模式.该篇文章,会以图文讲解的方式带你彻底掌握Activity的启动 ...
- 最优秀的网络框架retrofit
由于某学员要求所以我打算写一篇 标题先记录下来 我会在一周内完成此篇文章
- 从1....n中随机输出m个不重复的数
void knuth(int n, int m) { srand((unsigned) time( NULL)); for (int i = 0; i < n && m; i++ ...
- Python 3 函数自由变量的大坑
Python中函数是一个对象, 和整数,字符串等对象有很多相似之处,例如可以作为其他函数的参数或返回对象, Python中的函数还可以携带自由变量, 两者无疑极大增进了Python的表达力. 但是Py ...
- 用reg文件把便携版sublime text 3添加到右键菜单
假设sublime文件夹在C:\\Users\\T430i\\Downloads\\Sublime Text Build 3059 x64\\ 则: Windows Registry Editor V ...
- Linux 高性能服务器编程——高级I/O函数
重定向dup和dup2函数 #include <unistd.h> int dup(int file_descriptor); int dup2(int file_descriptor_o ...
- 剑指Offer——小米+小红书笔试题+知识点总结
剑指Offer--小米+小红书笔试题+知识点总结 情景回顾 时间:2016.9.23 19:00-21:00 2016.9.24 15:00-17:00 地点:山东省网络环境智能计算技术重点实验室 事 ...
- Android Firebase 服务简介
Firebase初步了解 什么事Firebase?Firebase成立于2011年,在被Google收购之前,Firebase是一个协助开发者快速构建App,能够提供行动应用专用开发平台及SDK的一款 ...
- Struts 2之Servlet API
Struts 2对Servlet API进行了封装,是业务层更加独立,如果需要调用Request.Response等Servlet API有两种途径 利用ServletActinContext的静态方 ...