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. Android中使用WebView, WebChromeClient和WebViewClient加载网页

    在android应用中,有时要加载一个网页,如果能配上一个进度条就更好了,而android 中提供了其很好的支持, 其中webView的一系列用法,比如 webView.getSettings().s ...

  2. ios8/sdk8/xcode6/iphone6(+)适配

    AppIcon https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Ic ...

  3. Object-c 语法 - 头文件引用(@class/#import/#include)

    一. Objective-C 中 #import 和 #include 的区别 预编译指令 Objective-C:#import:由gcc编译器支持 C,C++:#include 在 Objecti ...

  4. Java实现Internet地址获取

    Java实现Internet地址获取 代码内容 输入域名输出IPV4地址 输入IP地址输出域名 支持命令行输入 支持交互式输入 代码实现 /* nslookup.java */ import java ...

  5. 【转载】VGA时序与原理

    显示器扫描方式分为逐行扫描和隔行扫描:逐行扫描是扫描从屏幕左上角一点开始,从左像右逐点扫描,每扫描完一行,电子束回到屏幕的左边下一行的起始位置,在这期间,CRT对电子束进行消隐,每行结束时,用行同步信 ...

  6. sublime mac快捷键

    ^是control ⌥是option 打开/前往 ⌘T 前往文件 ⌘⌃P 前往项目 ⌘R 前往 method ⌘⇧P 命令提示 ⌃G 前往行 ⌘KB 开关侧栏 ⌃ ` python 控制台 ⌘⇧N 新 ...

  7. 【BZOJ 1033】 [ZJOI2008]杀蚂蚁antbuster

    Description 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试图把蛋糕搬回蚂蚁窝.而你的任 ...

  8. [Android Training视频系列] 8.3 Dealing with Audio Output Hardware

    用户在播放音乐的时候有多个选择,可以使用内置的扬声器,有线耳机或者是支持A2DP的蓝牙耳机.(补充:A2DP全名是Advanced Audio Distribution Profile 蓝牙音频传输模 ...

  9. 关于网站IIS日志分析搜索引擎爬虫说明

    正文:iis默认的日志文件在C:\WINDOWS\system32\LogFiles中,下面是Seoer惜缘的服务器日志,通过查看,就可以了解搜索引擎蜘蛛爬行经过,如: 2008-08-19 00:0 ...

  10. android 自定义折线图

    看图: 比较简陋,主要是通过canvas画上去的: package com.example.democurvegraph.view; import java.util.ArrayList; impor ...