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. NC V6 安装目录各文件夹作用描述

    ant:存放Apache Ant,用来执行EJB的构建. bin: 存放nc部署和系统监控等命令.configsys.log部署日志(包含NC中间件.WAS中间件等部署信息)以及NC_Client文件 ...

  2. asp.net身份认证

    在网上看到几篇比较好的文章很详细讲解了Form.Membership.以及Identity身份认证 Form身份认证: http://www.cnblogs.com/fish-li/archive/2 ...

  3. s3c6410_中断

    参考: 1)<USER'S MANUAL-S3C6410X>Chapter 10 GPIO, Chapter 12 VECTORED INTERRUPT CONTROLLER 2)< ...

  4. Connect to a Windows PC from Ubuntu via Remote Desktop Connection

    http://www.7tutorials.com/connecting-windows-remote-desktop-ubuntu A useful feature of Windows is be ...

  5. NSLog的使用

    1.NSLog会将字符串打印在两个地方:操作系统的控制台和IDE的控制台. 2.对于NSLog来说,它打印的一个目的地并非控制台,而是系统文件“system.log”. 它另一个输出的目的地并非IED ...

  6. iPhone和iPad版本的分辨率a

  7. silverlight获取web的url参数

    1.网址(如:http://localhost:8081/index.aspx?name=123) 2.获取name=123的信息 3.IDictionary<string,string> ...

  8. Linux下mysql编译安装

    一.下载安装包 因为编译安装mysql需要cmake所以先编译安装cmake,这里都一起下载下来 下载cmake,网址https://cmake.org/download/ 下载mysql,网址htt ...

  9. 单个input框上传多个文件操作

    HTML页面: <div class="form-group thumb"> <label class="control-label col-xs-12 ...

  10. activiti

    http://activiti.org/designer/archived/  eclipse plugin