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()的更多相关文章

  1. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

  3. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  4. [LeetCode]题解(python):028-Implement strStr()

    题目来源: https://leetcode.com/problems/implement-strstr/ 题意分析: 输入两个字符串haystack和needle,如果needle是haystack ...

  5. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

  6. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. 【算法】LeetCode算法题-Implement strStr

    这是悦乐书的第151次更新,第153篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第10题(顺位题号是28).给定两个任意字符串haystack.needle,返回hay ...

  8. Leetcode : eImplement strStr

    Leetcode : eImplement strStr 描述 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个 ...

  9. 乘风破浪:LeetCode真题_028_Implement strStr()

    乘风破浪:LeetCode真题_028_Implement strStr() 一.前言     这次是字符串匹配问题,找到最开始匹配的位置,并返回. 二.Implement strStr() 2.1 ...

随机推荐

  1. Java实现多线程邮件发送

    利用java多线程技术配合线程池实现多任务邮件发送. 1.基本邮件发送MailSender package hk.buttonwood.ops.email; import java.io.File; ...

  2. ios中怎么样设置drawRect方法中绘图的位置

    其中drawRect方法中的参数rect就是用来设置位置的,

  3. 单点登录 关于Cookie跨域的问题

    public void ProcessRequest(HttpContext context) { HttpCookie cookie = new HttpCookie("name" ...

  4. angularJS项目-ajax事件的按钮loading和页面loading状态 & Controller之间通信-待续

    1).按钮loading --TODO 2). page loading状态 1.在module中注入指令 // Route State Load Spinner(used on page or co ...

  5. FPGA仿真设置步骤

    1.FPGA仿真流程图 2.FPGA时序图 3.FPGA开发方式

  6. mini6410-JNI-led

    一.编写JNI模块 当安装好NDK编译环境后,会在它的目录下找到sample目录,它里面有一些例子,可以参考这些例子来写我们自已的模块. 1.在/home/android/文件夹下,新建“ledjni ...

  7. MATLAB 利用filter函数实现滑动平均滤波

      function [ y ] = moving_average( x, win_size ) y1=filter(ones(1,win_size/2+1)/win_size,1,x); y2=fi ...

  8. 设置搜狗输入法在任何时候按左右两侧的shift激活

    如上图,搜狗输入法for linux最近与广大用户见面了,现在的版本是1.0.0.0014,本人系统是ubuntu 14.04非麒麟版本 使用过程中有个习惯就是在任何窗口内只要按任意一侧的shift就 ...

  9. 【BOZJ 1901】Zju2112 Dynamic Rankings

    Description 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是 ...

  10. 思维认知-读mindhacks杂记

    1. 导语 无意中浏览知乎,搜索到了mindhacks.cn这个个人geek的网址.mindhacks博主本人是牛人程序员一枚,但他的博客主题涵盖的主要内容确是思维改变生活. 博客链接地址:http: ...