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 ...
随机推荐
- [JavaCore] 不错的Java基础学习资料-持续更新
容易弄混的JAVA基础知识: http://www.iteye.com/topic/943647 [总结]String in Java: http://www.iteye.com/topic/5221 ...
- Hbuilder连接模拟器调试
Hbuilder是一个非常好用的HTML5开发IDE,我最喜欢的功能就是连接手机调试了,连接手机调试有两种途径,一是通过USB连接真机,二是下载安装一个安卓模拟器,让Hbuilder连接到安卓模拟器, ...
- Iphone [Tab Bar实现多view切换,Picker,DataPicter实现
用Tab Bar Controller处理IPhone多个view切换, 而且还附有创建空项目,picker和DataPicker的实现! 具体步骤: 1.创建一个空项目,选择User Interfa ...
- 注解:【有连接表的】Hibernate单向N->N关联
Person与Address关联:单向N->N,[有连接表的] #和单向1->N关联代码完全相同,控制关系的一端需要增加一个set类型的属性,被关联的持久化实例以集合形式存在. #N-&g ...
- Java Socket编程(转)
Java Socket编程 对于Java Socket编程而言,有两个概念,一个是ServerSocket,一个是Socket.服务端和客户端之间通过Socket建立连接,之后它们就可以进行通信了.首 ...
- loj 1210 (求最少的加边数使得图变成强连通)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1210 思路:首先是缩点染色,然后重建并且统计新图中的每个点的入度和出度,于是答案就是m ...
- document.body / document.ducumentElement /等获取高度和宽度的区别 ----转载
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 智能车学习(二十)——浅谈C车硬连接与软连接
一.为何要追求软连接? 车子进行软连接之后,可以达到一种效果,就是在高速过程中,车子如果快要发生侧翻的时候,只会跳一个后轮,且只是轻微,而前轮如果进行的内倾,就可以让前轮最大面积接触,增大 ...
- Hbase原理、基本概念、基本架构
来源:http://blog.csdn.net/woshiwanxin102213/article/details/17584043 概述 HBase是一个构建在HDFS上的分布式列存储系统:HBas ...
- 【File】递归删除文件夹中子级文件/夹,并删除文件夹
今天有这样一个需求,需要删除某一个文件夹,但是文件夹中还有子级的文件 或者还可能会有文件夹在里面,所以就需要使用一个简单的递归才能将文件夹删除成功,包括文件夹中的子级文件/夹.!!! 其实很简单,就一 ...