[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 ...
随机推荐
- easyui treegrid逐步加载
$("#bomStructureTable").treegrid({ url : "systemcontroller?id=10007",//首次查询路径 qu ...
- vs命令行采集性能数据
VSPerfASPNETCmd /output:d://PerformanceReport.vsp http://localhost/store/cart.aspx
- 用grunt进行前端工程化之路
我们的项目wecash4.0的前端构建考虑过用fis和grunt. 目录: 前期调研:fis vs grunt vs gulp? 一.安装grunt和项目. fis是百度fex研发的构建工具,非常方便 ...
- 使用angularJS遇见的一些问题的解决方案
1. angularJS的$http.post请求,SpringMVC后台接收不到参数值的解决方案 问题一般为:400 Required String parameter 'rPassword' is ...
- onethink和thinkphp3.2学习
thinkphp发布3.2版本之后,也发布了一个简单的内容管理系统onthink,这样有助于理解thinkphp3.2的使用: 一.首先最关键的一点是thinkphp3.2中加入了命名空间的使用 什么 ...
- 下载Spring框架开发包
1.打开官网: http://spring.io/,打开project >> spring framework 2.在右侧找到要用的版本,如4.3.4,打开reference,搜索&quo ...
- easyui自定义标签 datagrid edit combobox 手动输入保存不上问题解决办法
使用onEndEdit事件(该事件可以获取到editor对象,onAfterEdit事件获取不到Editor对象) 通过editor拿到输入数据并保存. int ci = 0; for(Column ...
- linux mysql操作命令
1.linux下启动mysql的命令:mysqladmin start/ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令:mysqla ...
- UI基础之UITextField相关
UITextField *textF = [[UITextField alloc] init]; 1.字体相关 textF.text = @"文本框文字"; textF.textC ...
- vim的跨文件复制粘贴
1.用vim打开一个文件,例如:a.cpp 2.在普通模式下,输入:":sp"(不含引号)横向切分一个窗口,或者":vsp"纵向切分一个窗口,敲入命令后,你将看 ...