Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char * or String, please click the reload button to reset your code definition.

Solution:

 public class Solution {
public int strStr(String haystack, String needle) {
if (needle.isEmpty()) return 0;
if (haystack.isEmpty()) return -1; int[] pm = getPartialMatchTable(needle);
int p1 = 0, p2 = 0;
while (p1<haystack.length() && p2<needle.length()){
if (haystack.charAt(p1)==needle.charAt(p2)){
p1++;
p2++;
} else {
if (p2==0) p1++;
else p2 = pm[p2-1]+1;
}
}
if (p2<needle.length()) return -1;
else return p1-p2; } public int[] getPartialMatchTable(String needle){
int[] pm = new int[needle.length()];
pm[0] = -1;
for (int i=1;i<needle.length();i++){
int j = pm[i-1];
while (j>=0 && needle.charAt(i)!=needle.charAt(j+1)) j = pm[j];
if (needle.charAt(i)==needle.charAt(j+1)) pm[i] = j+1;
else pm[i] = -1;
}
return pm;
}
}

LeetCode-Implement strStr()-KMP的更多相关文章

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

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

  2. LeetCode Implement strStr()(Sunday算法)

    LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...

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

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

  4. [LeetCode] Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  5. LeetCode: Implement strStr() [027]

    [题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...

  6. leetcode——Implement strStr() 实现字符串匹配函数(AC)

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  7. 28. Implement strStr()(KMP字符串匹配算法)

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

  8. 28.Implement strStr()---kmp

    题目链接:https://leetcode.com/problems/implement-strstr/description/ 题目大意:字符串匹配,从字符串中,找到给定字符串第一次出现的位置下标, ...

  9. leetcode implement strStr python

    #kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...

  10. LeetCode Implement strStr() 实现strstr()

    如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...

随机推荐

  1. Windows server 共享文件夹权限设置

    概念:共享权限应用于通过网络连接到共享文件夹的用户.共享权限不会影响本地登录或使用远程桌面登录的用户. 若要为本地登录用户或使用远程桌面登录的用户设置权限,请使用“安全”选项卡而不是“共享权限”选项卡 ...

  2. 一个简单的SpringMVC3 程序

    初学者对于Spring框架的难度:引用Jar包不全,或者不正确: 1.运行界面 2.客户端页面 index.jsp 的代码 <%@ page language="java" ...

  3. 006NFS与TFTP服务器

    1.交叉开发:嵌入式系统开发多采用交叉开发模式,其中产生嵌入式软件的平台称为宿主机,通常为PC电脑,运行嵌入式软件的平台称为目标机.宿主机一般通过网络,USB,JTAG等方式将软件下载到目标机. 2. ...

  4. 1.4Linux内核版本号的定义规则

    Linux内核版本号的组成: (1)主版本号: (2)次版本号: (3)修订版本号: (4)微调版本号: (5)为特定的Linux系统特别调校的描述: 例子:2.6.29.7-flykernel-12 ...

  5. Android开发教程 录音和播放

    首先要了解andriod开发中andriod多媒体框架包含了什么,它包含了获取和编码多种音频格式的支持,因此你几耍轻松把音频合并到你的应用中,若设备支持,使用MediaRecorder APIs便可以 ...

  6. 当局部变量遇上全局变量——extern及花括号用法举例

    请阅读以下代码并说出它的输出结果. #include <stdio.h> ; int foo() { ; { extern int val; printf("val_foo = ...

  7. ADO .NET 链接 增删改查

    ADO.NET: 数据访问技术 就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中也可以将数据库中的数据提取到内存中供程序调用 所有数据访问技术的基础 连 ...

  8. Silverlight - GPU加速

    1. 在Silverlight plug-in上设置 <param name="enableGPUAcceleration" value="true" / ...

  9. 通过Eclipse创建SQLite数据库

    import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database ...

  10. Show or Hide Menu List via ng-show

    <div ng-app ng-controller='MenuController'> <ul ng-show='menuState_show'> <li>Stun ...