LeetCode-Implement strStr()-KMP
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的更多相关文章
- [Leetcode] implement strStr() (C++)
Github leetcode 我的解题仓库 https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...
- LeetCode Implement strStr()(Sunday算法)
LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- LeetCode: Implement strStr() [027]
[题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...
- leetcode——Implement strStr() 实现字符串匹配函数(AC)
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- 28. Implement strStr()(KMP字符串匹配算法)
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- 28.Implement strStr()---kmp
题目链接:https://leetcode.com/problems/implement-strstr/description/ 题目大意:字符串匹配,从字符串中,找到给定字符串第一次出现的位置下标, ...
- leetcode implement strStr python
#kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...
- LeetCode Implement strStr() 实现strstr()
如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...
随机推荐
- NC V6 安装目录各文件夹作用描述
ant:存放Apache Ant,用来执行EJB的构建. bin: 存放nc部署和系统监控等命令.configsys.log部署日志(包含NC中间件.WAS中间件等部署信息)以及NC_Client文件 ...
- asp.net身份认证
在网上看到几篇比较好的文章很详细讲解了Form.Membership.以及Identity身份认证 Form身份认证: http://www.cnblogs.com/fish-li/archive/2 ...
- s3c6410_中断
参考: 1)<USER'S MANUAL-S3C6410X>Chapter 10 GPIO, Chapter 12 VECTORED INTERRUPT CONTROLLER 2)< ...
- 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 ...
- NSLog的使用
1.NSLog会将字符串打印在两个地方:操作系统的控制台和IDE的控制台. 2.对于NSLog来说,它打印的一个目的地并非控制台,而是系统文件“system.log”. 它另一个输出的目的地并非IED ...
- iPhone和iPad版本的分辨率a
- silverlight获取web的url参数
1.网址(如:http://localhost:8081/index.aspx?name=123) 2.获取name=123的信息 3.IDictionary<string,string> ...
- Linux下mysql编译安装
一.下载安装包 因为编译安装mysql需要cmake所以先编译安装cmake,这里都一起下载下来 下载cmake,网址https://cmake.org/download/ 下载mysql,网址htt ...
- 单个input框上传多个文件操作
HTML页面: <div class="form-group thumb"> <label class="control-label col-xs-12 ...
- activiti
http://activiti.org/designer/archived/ eclipse plugin