[leetcode 27]Implement strStr()
1 题目:
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.
2 思路:
估计是要实现KMP算法。
正好我从大二就开始想把它看懂。。。
这次搞明白了。
我是 谷歌 “KMP 通俗”,前面有三个帖子,看完后,差不多才懂得。
3 代码:
/* KMP method */
//
public int strStr(String haystack, String needle) {
// pre handle
if( needle.length()==0){
return 0;
}
if(haystack.length()==0){
return -1;
} char[] target = haystack.toCharArray();
char[] point = needle.toCharArray(); // build the point array, the last feature[len-1] not satify the sub-prifix & sub-suffix rule, but not matter,the feature[len-1] will not be use
Integer[] feature = new Integer[needle.length()];
feature[0] = 0;
for(int i=1; i<needle.length()-1; i++){
if(point[i]!=point[feature[i-1]]){
feature[i] = 0;
}else{
feature[i] = feature[i-1]+1;
}
} // search
int j = 0;
for(int i=0; i<haystack.length();){
if(target[i]==point[j]){
j++;
if(j == needle.length()){
// match, return index
return i-j+1;
}
i++;
}else{
if(j == 0){
/* j=0 not match,i++ */
i++;
}else{
/* not match, continue to compare target[i] */
j = feature[j-1];
}
}
} return -1;
}
[leetcode 27]Implement strStr()的更多相关文章
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- 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() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- Leetcode 28——Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- 关于MVC中View使用自定义方法
今天学习到了在MVC的View中使用自定义方法,很简单,下面分享一下. 1.首先在项目下面建立一个文件夹,用于存我们写的自定义方法. 2.在新建文件夹中新增一个类,命名随便取(最好还是和自定义方法关联 ...
- Oracle数据库导入导出命令总结 (详询请加qq:2085920154)
分类: Linux Oracle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的 ...
- Linux命令小结:crontab/netstat/iostat/sar
crontab cron可以设定在指定的时间运行任务. 1.查看定时任务 [root@client1 ~]# crontab -l -u root */1 * * * * date >> ...
- FreeMarker标签与使用
模板技术在现代的软件开发中有着重要的地位,而目前最流行的两种模板技术恐怕要算freemarker和velocity了,webwork2.2对两者都有不错的支持,也就是说在webwork2中你可以随意选 ...
- python json模块
import json dic = {"name":"liao","age":18} data = json.dumps(dic) #转化为 ...
- Hibernate缓存之Aop+cache
在上一篇涉及到查询缓存的功能时除了需要在配置文件中开启缓存外,还需要在业务代码中显示调用setCacheable(boolean)才可以打开查询缓存的功能,这样做,无疑是破坏了封装性,所以就诞生了利用 ...
- c# 筛选进程命令行,得其ProcessId(唯一标示符,简称pid),再通过pid结束进程
不说别的,上代码 部分using: using System.Diagnostics; using System.Management; 其中要引用System.Management 1.通过筛选Co ...
- 【转】linux yum命令详解
yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指定的服务器自动下载RP ...
- 17.iOS App设置icon,启动图,App名称的方法
icon:选择Assets-->AppIcon-->将各种尺寸的icon拖拽到相应的框中. APP名称:选择info-->Bundle name,修改APP名字. 启动图: 首先点击 ...
- C#装箱和拆箱
1.装箱是将值类型转换为引用类型(或者转换为此值类型所实现的任何接口类型)的隐式转换,当 CLR 对值类型进行装箱时,会将该值包装到 System.Object 内部,再将后者存储在托管堆上. ; / ...