LeetCode: Implement strStr() [027]
【题目】
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
【题意】
实现库函数strStr(), 功能是在字符串haystack中找出目标串needle第一次出现的索引位
【思路】
字符串的匹配,能够用暴力解法,但不推荐。一般使用KMP算法求解。
简要介绍一下KMP的思想:
haystack是题设所给的匹配串,needle是题设所给的模式串。
needle在和haystack匹配时,假设遇到某个位置上的字符失配,暴力法会将needle的游标返回头部,从头開始又一次匹配;而KMP算法同意游标不返回头部,而利用已有的匹配结果,将游标移动到新的位置k上,从k位置開始匹配。
怎样确定这个新位置,或者说这个新位置是什么呢?
(1).我们这么来看,如果在当前的匹配过程中发现haystack[i]!=needle[j],显然这个时候haystack[i-j, i-1]和needle[0,j-1]各个位置上的字符已经匹配。
(2).此时,needle新的游标位置next[j]=k,它满足子串needle[0,k-1]与子串needle[j-k,j-1]全然匹配,也就是说字符串needle[0,j-1]的前k个字符与后k个字符同样。【k是满足该匹配条件的最大值,且k<j】
(3)把needle的游标从j移动到k,意味着我们从k開始比較就能够了,needle[0,k-1]不须要再反复比較了。这一点由(1)保证。
结合(1)(2)我们有:haystack[i-j, i-1]=needle[0,j-1] && needle[0,k-1]=needle[j-k, j-1]
显然我们能够推出:needle[0,k-1]=haystack[i-k,i-1]; 也即从k位置開始比較就可以,不须要从头開始匹配。
KMP算法关键在于建立next数组,即发生不匹配情况是回跳到哪个位置继续匹配。
如果我们已经确定了needle字符串中j位置的字符失配时跳转的位置是k,即next[j]=k, 依据上面的分析我们知道此时满足needle[0,k-1]=needle[j-k, j-1]。那next[j+1]=?
1. 假设needle[k]==needle[j],显然有needle[0,k]=needle[j-k, j],则next[j+1]=k+1;
2. 假设needle[k]!=needle[j], 这也是一种失配的情况(即needle起始字符串和结尾字符串匹配时发生失配),则游标k应该跳转到next[k],然后再与needle[j]比較,假设还是失配,游标继续通过next数组回跳,直至与needle[j]匹配,此时next[j+1]=k+1;
【代码】
class Solution {
public:
void getNext(char*needle, int*next){
int size=strlen(needle);
int i=0,k=-1;
next[0]=-1;
while(i<size-1){ //由于我们通过A[i]来求A[i+1]的,因此结束条件是i<size-1
if(k==-1||needle[i]==needle[k]){
i++;
k++;
next[i]=k;
}
else{
k=next[k];
}
}
}
char *strStr(char *haystack, char *needle) {
if(needle==NULL || haystack==NULL)return NULL;
int patternSize=strlen(needle);
int haystackSize=strlen(haystack);
int*next=new int[patternSize];
//生成next数组
getNext(needle,next);
//匹配
int i=0; //haystack的游标
int j=0; //needle的游标
while(i<haystackSize&&j<patternSize){
if(j==-1||haystack[i]==needle[j]){
i++;j++;
}
else{
j=next[j];
}
}
delete next;
if(j==patternSize)return haystack+i-j;
return NULL;
}
};
LeetCode: Implement strStr() [027]的更多相关文章
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [Leetcode] implement strStr() (C++)
Github leetcode 我的解题仓库 https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...
- LeetCode Implement strStr()(Sunday算法)
LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...
- [LeetCode] Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- leetcode——Implement strStr() 实现字符串匹配函数(AC)
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- leetcode implement strStr python
#kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...
- LeetCode Implement strStr() 实现strstr()
如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
随机推荐
- linux/mac下命令行rm回收站--rmtrash
Linux.mac的命令行下没有回收站功能,很多时候手一抖就把重要文件给 rm -fr * 了,虽然linux下有可能通过lost +found/debugfs找回,但难度也比较大,不能保证一定能够找 ...
- 使用GNU工具链进行嵌入式裸机开发
Embedded-Programming-with-the-GNU-Toolchain Vijay Kumar B. vijaykumar@bravegnu.org 翻译整理:thammer gith ...
- 三种显著性检测算法(SR,HFT,GBMR)
一.谱残差(Spectral Residual, SR) 一种简单的图像显著性计算模型 http://www.cnblogs.com/CCBB/archive/2011/05/19/2051442. ...
- 在静态方法中应用spring注入的类
最近在一次项目的重构中,原项目需要在静态方法中调用service,现在需要更换框架,service需要自动注入,无法再静态方法中调用 解决思路: 创建一个当前类的静态变量,创建一个方法,使用@Post ...
- Linux基础命令—网卡
#1.实时查看网卡流量 #sar -n DEV 1 5 [每间隔1秒刷新一次,共5次] sar -n DEV 1 5 IFACE 表示设备名称 rxpck/s 每秒接收的包的数量 txpck/s 每秒 ...
- JAVAEE——宜立方商城03:商品类目选择、Nginx端口或域名区分虚拟机、Nginx反向代理、负载均衡、keepalived实现高可用
1. 学习计划 第三天: 1.商品类目选择(EasyUI的tree实现) 2.图片上传 a) 图片服务器FastDFS(Nainx部分) 2. 商品类目选择 2.1. 原型 2.2. 功能分析 展示商 ...
- Date日期
当我们只需要一个日期时,或从系统取得,或从数据库查询,都可以放入一个Date对象. 当我们需要对Date进行详细分析,获取其中的年月日分秒各个部分的信息,用Calendar类. 当我们需要对一个字符串 ...
- 理解事件(Event)
Overview 在前几章,我们已经对委托有了一个完整的了解了,本章将会对事件进行一下介绍: 相对于委托,事件再是我们更加频繁的接触的,比如 鼠标的click 事件,键盘的 keydown 事件等等. ...
- Django框架(一)-Django初识
Django初识 一.Web框架本质—自己实现Web框架 1.所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端 import socket sk = sock ...
- Codeforces Round #493 (Div 2) (A~E)
目录 Codeforces 998 A.Balloons B.Cutting C.Convert to Ones D.Roman Digits E.Sky Full of Stars(容斥 计数) C ...