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 ...
随机推荐
- jquery最精简的全选反选功能
RT代码: function selallno(){ $('#form2 input[name=sel]:checkbox:not(:checked)').attr('checked',$('#for ...
- linux 101 hacks 4stat diff ac
stat 命令 stat 命令那个可以用来查看文件或者文件系统的状态和属性.显示一个文件或目录的属性 $ stat /etc/my.cnf File: `/etc/my.cnf' Size: Bloc ...
- spring boot之org.springframework.boot.context.TypeExcludeFilter
曾经碰到过这样一种情况,想让某个使用了spring 注解的类不被spring扫描注入到spring bean池中,比如下面的类使用了@Component和@ConfigurationPropertie ...
- python3下django连接mysql数据库
1.安装pymysql pip install pymysql 有一点需要注意,有的系统(比如ubuntu16.04)同时安装了python2和python3,而比较新的django需要在python ...
- odoo 模型与ORM
型号属性 在/模型添加activity.py文件 class ActivityEvent(models.Model): _name = 'activity.event' _inherit = 'eve ...
- Entity Framework Core 入门(2)
安装 EF Core 将 EF Core 添加到不同平台和常用 IDE 中的应用程序的所需步骤汇总. 分步入门教程 无需具备 Entity Framework Core 或任何特定 IDE 的原有知识 ...
- 1006 Sign In and Sign Out (25)(25 point(s))
problem At the beginning of every day, the first person who signs in the computer room will unlock t ...
- 【WIN10】VisualStateManager使用說明
Demo下載:http://yunpan.cn/cFjgPtWRHKH9H 访问密码 c4b7 顧名思義,視圖狀態管理器. 在WPF中,它的功能似乎更強大.在UWP中,閹割了GotElementSt ...
- 【BZOJ 2721】 2721: [Violet 5]樱花 (筛)
2721: [Violet 5]樱花 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 599 Solved: 354 Description Input ...
- Centos7(Firewall)防火墙开启常见端口命令
使用云服务器的,一定要注意开启安全组配置的响应端口 Centos7默认安装了firewalld,如果没有安装的话,则需要YUM命令安装:firewalld真的用不习惯,与之前的iptable防火墙区别 ...