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 ...
随机推荐
- Android SQLite数据储存方式
SQLiteOpenHelper 类 用SQLiteOpenHelper 类中的 getWritableDatabase()和getReadableDatabase()方法可以获得数据库的引用. 为了 ...
- 图解Javascript上下文与作用域
原文网址:http://blog.rainy.im/2015/07/04/scope-chain-and-prototype-chain-in-js/ 本文尝试阐述Javascript中的上下文与作用 ...
- ★★★.NET 在meta标签中使用表达式设置页面的关键字
在aspx文件中 给meta标签的属性复制是不能直接使用 表达式的 错误的写法: <meta name="keywords" content="<%=news ...
- Android开发中Ant命令编译和APK签名的一些心得
本文章麦子学院跟小伙伴们详细的分享一下关于Android Ant命令行编译和APK签名详解一些实现方法,这是一个朋友在自己做安卓开发时写的,希望对大家会有所帮助呀. 最近在做Android开发时,需要 ...
- 网络基础知识、ASP.NET 核心知识(1)*
为什么要写网络? 我原本的计划是这样的,连续两天梳理ASP.NET开发的核心知识.说到这呢,有人问了.“不是说好了做ASP.NET笔记吗?为啥要写网络基础知识?是不是傻?” 原因是这样的.作为网站开发 ...
- What is the difference between differed processing mode and interactive mode?
Every time you access and navigate through the fields on a page in PeopleSoft there are events such ...
- ElasticSearch 模板文件配置
首先是推荐一下参考资料 中文资料:http://kibana.logstash.es/content/elasticsearch/index.html 官方文档:https://www.elastic ...
- 学习c编程的第三天
#include<stdio.h> int add(int,int); int main(){ int x=add(1,2); printf("%d",x); retu ...
- HBase分布式安装
安装HBase之前需要先安装Hadoop,因为HBase是运行在Hadoop集群上的.安装Hadoop可以参照http://www.cnblogs.com/stGeekpower/p/3307289. ...
- IoC~高效的Autofac
http://www.cnblogs.com/lori/archive/2012/09/05/2671724.html http://www.cnblogs.com/kissdodog/p/36114 ...