Implement strStr()
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.
算法:KMP算法,字符串比对
java:
public class Solution {
public int strStr(String haystack, String needle) {
int l1=haystack.length();
int l2 = needle.length();
if(l1<l2){
return -1;
}
if(l1==l2){
if(haystack.equals(needle)){
return 0;
}
return -1;
}
if(l2==0){
return 0;
}
int i=0;
int j=0;
int[] next = new int[l2+1];
getNext(needle,next);
while(i<l1&&j<l2){
if(j==-1||haystack.charAt(i)==needle.charAt(j)){
i++;
j++;
}else{
j = next[j];
}
if(j==l2){
return i-l2;
}
}
return -1;
}
public void getNext(String s, int[] next){
int i,j;
int len = s.length();
i=0;
j=-1;
next[0]=-1;
while(i<len-1){
if(j==-1||s.charAt(i)==s.charAt(j)){
i++;
j++;
next[i]=j;
}else{
j=next[j];
}
}
}
}
Implement strStr()的更多相关文章
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 28. Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- 【leetcode】Implement strStr() (easy)
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 ...
- Implement strStr() [LeetCode]
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- 70. Implement strStr() 与 KMP算法
Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...
随机推荐
- set[c++]
#include <iostream> using namespace std; #include <set> int main(int argc, const char * ...
- Android三种基本的加载网络图片方式(转)
Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 ...
- 用计算器计算“异或CRC”
再计算器上输入以下数字,每输入一个数字,按一下“Xor”
- JAVA的容器---List,Map,Set (转)
JAVA的容器---List,Map,Set Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashM ...
- cocos2dx游戏开发——微信打飞机学习笔记(二)——游戏框架
一.游戏的基本框架: WelcomeScene ——> GameScene ——> GameOverScene || ...
- 学习linux内核时常碰到的汇编指令(2)
转载:http://blog.sina.com.cn/s/blog_4be6adec01007xvh.html JNGE∶指令助记符——(有符号数比较)不大于且不等于转移(等价于JL).当SF和OF异 ...
- 【SQL Sever】将SQL Sever中的一个数据表的数据导出为insert语句
例如:这SQL Sever中的一张数据表,想要将这张数据表中的数据 转化成一个一个的insert语句存储在txt的文档中,那么不论走到那里这个insert语句一执行,我们就能将这个数据表中的数据 ...
- spring实例教程
1.配置好spring mvc发现访问无法匹配,很可能是文件放的位置或者相对目录不对. 2.实例大全:http://www.yiibai.com/spring/spring-collections-l ...
- LoadRunner录制图片验证码
LoadRunner录制图片验证码 LoadRunner自身是无法捕获到图片验证码的,但是我们可以帮助LoadRunner来实现验证码的捕获. 1.图片验证码 图片验证码的产生来自服务器端,由服务器生 ...
- js高级技巧之高级定时器
实际上,浏览器负责进行排序,指派某段代码在某个时间点运行的优先级. 可以吧js想象成在时间线上运行的. JavaScript中没有任何代码是立刻执行的,但一旦进程空闲则尽快执行. 1.重复的定时器: ...