Leetcode: strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
分析:该题是经典的串匹配,我们可以用KMP来解,时间复杂度为O(m+n)。关于KMP的详细内容可以参考一下博文http://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html。这里,我主要介绍一下运用KMP的几个关键点。
1. 为了避免指针回溯,KMP引入了next数组,用来确定下次匹配时模式串指针的位置。在用next数组前,我们要知道next[j]的含义,便于我们理解和实现。通俗的讲,next[j]表示pattern[0,j-1]中其前缀跟后缀相同的最大长度,我们用下面的式子来帮助理解:
next[0] = -1, next[1] = 0; for j > 1, next[j] = max(k) where 0<k<j and pattern[0,k-1] = pattern[j-k, j-1]。
2. 如何计算next数组,我们可以用动态规划的思想来计算next数组,在计算next[j]时,如果pattern[j-1] = pattern[next[j-1]],那么next[j] = next[j-1] + 1; 否则不匹配,则可以按KMP的做法,用next[j-1]确定下一个匹配的位置(此时模式串和目标串都是pattern[0,j-1])。
3. 在解决上面两个问题后,我们讨论如果通过next数组来做串匹配。在串匹配的时候可分两种情况:
1) target[i] = pattern[j],说明匹配,我们只需i++, j++。
2)target[i] != pattern[j], 此时我们需要用next数组确定j的下一个匹配位置。如果next[j] >= 0,则 j = next[j],i位置不便; 如果next[j] == -1,i往后移一步,j置0。
在实现时,2)中next[j] = -1的情况可以跟1)的情况合并。
解决了上面三个讨论,我们就可以写出KMP代码了,如下:
class Solution {
public:
char *strStr(char *haystack, char *needle) {
return kmp(haystack, needle);
}
char * kmp(char * haystack, char * needle){
int m = strlen(needle);
if(m == ) return haystack;
int * next = (int *)malloc(sizeof(int) * m);
compute_next(needle, next);
int i = , k = ;
while(i < strlen(haystack)){
if(k == - || haystack[i] == needle[k]){
k++;
i++;
}else k = next[k];
if(k == m) return haystack+i-m;
}
return NULL;
}
void compute_next(char * needle, int * next){
int m = strlen(needle);
next[] = -;
int k = -;
for(int j = ; j < m-;){
if(k == - || needle[j] == needle[k]){
k++;
j++;
next[j] = k;
}else k = next[k];
}
}
};
Leetcode: strStr()的更多相关文章
- [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][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- [LeetCode]题解(python):028-Implement strStr()
题目来源: https://leetcode.com/problems/implement-strstr/ 题意分析: 输入两个字符串haystack和needle,如果needle是haystack ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- 【算法】LeetCode算法题-Implement strStr
这是悦乐书的第151次更新,第153篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第10题(顺位题号是28).给定两个任意字符串haystack.needle,返回hay ...
- Leetcode : eImplement strStr
Leetcode : eImplement strStr 描述 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个 ...
- 乘风破浪:LeetCode真题_028_Implement strStr()
乘风破浪:LeetCode真题_028_Implement strStr() 一.前言 这次是字符串匹配问题,找到最开始匹配的位置,并返回. 二.Implement strStr() 2.1 ...
随机推荐
- EXCLE使用宏生成目录
宏代码: Sub mu() Dim i As Integer Dim ShtCount As Integer Dim SelectionCell As Range ShtCount = Workshe ...
- Nginx服务器架构简析
一.Nginx的模块化 模块化结构的思想是一个很久的概念,但也正是成熟的思想造就了Nginx的巨大优越性. 我们知道Nginx从总体上来讲是有许多个模块构成的.习惯将Nginx分为5大模块分别为:核心 ...
- 源码解析之setContentView
分享一下我以前学习时做到PPT中的一部分. 不难看出其实设置布局是由PhoneWindow来完成的. 然而我们要记住DecorView: 包括:状态栏,标题栏,内容 phoneWindow -> ...
- hdu 1381 Crazy Search
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1381 Crazy Search Description Many people like to sol ...
- golang:interface{}类型测试
在golang中空的interface即interface{}可以看作任意类型, 即C中的void *. 对interface{}进行类型测试有2种语法: 1. Comma-ok断言: value, ...
- sychronized面试问题浅析
先说下面试吧,整体来说基础准备好点,简历别太假,然后回答起来实事求是,表现自然的点基本上都没问题吧(针对初级职位,记得有个hr说过对于新人基础扎实和为人真诚是最关键的),两天时间跑起来挺累,反而觉得面 ...
- java 切换
Android L之后推荐使用JDK7编译程序,这是自然发展规律,就像是4年前编译Android 1.6需要使用JDK5一样. 多版本JDK是可以共存的,只需要使用update-alternative ...
- 006--VS2013 C++ 加载其他格式图片,并显示半透明化
//--------------------------------------------MyPaint() 函数------------------------------------------ ...
- 44.do文件格式
style1: transcript onif {[file exists rtl_work]} {<span style="white-space:pre"> < ...
- Java Day 11
异常 Throwable 子类 Error.Exception Error - 不可以处理 Exception - 针对性处理 原理 自定义异常 异常类的抛出throws 先检查语法错误,后检查逻辑 ...