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 ...
随机推荐
- visio 交叉线 不出现拱形怎么办?
- C语言文件操作fclose在NDK引起的BUG
今天在NDK中写了一个简单的写入文件操作: FILE *fp = fopen("/sdcard/test.txt","w"); if(fp == NULL) { ...
- 一张图入门python
- 【转】清理Kylin的中间存储数据(HDFS & HBase Tables)
http://blog.csdn.net/jiangshouzhuang/article/details/51290399 Kylin在创建cube过程中会在HDFS上生成中间数据.另外,当我们对cu ...
- 【转】Kylin的cube模型
转自:http://www.cnblogs.com/en-heng/p/5239311.html 1. 数据仓库的相关概念 OLAP 大部分数据库系统的主要任务是执行联机事务处理和查询处理,这种处理被 ...
- [转载]C++声明和定义的区别
<C++Primer>第四版 2.3.5节中这么说到: ①变量定义:用于为变量分配存储空间,还可为变量指定初始值.程序中,变量有且仅有一个定义. ②变量声明:用于向程序表明变量的类型和名字 ...
- PHP入门 - - 07-->HTML的表单
一.<form>标签及其属性 <from></form>标签对用来创建一个表单,即定义表单的开始和结束位置,<form>标签具有下面等属性. ...
- CSS3设置多张背景图片
background-image:url("1.jpg"),url("2.jpg"),url("3.jpg");background-rep ...
- 快销品 车销批发管理手持终端PDA系统 打印开单 入库 库存 盘点多功能一体
手持POS终端PDA移动开单 PDA通过扫描商品条码移动开单,实现便携式办公,伴随式销售,浩瀚技术研发团队开发的一款最新产品,PDA能通过WIFI无线局域网.GPRS互联网直接与主机连接,让公司业务人 ...
- 创建com服务器
Delphi Com深入编程 第二章: