[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 ...
随机推荐
- mysql 用命令操作
本篇文章来源于http://c.biancheng.net/cpp/html/1441.html mysql:连接数据库 mysql命令用户连接数据库. mysql命令格式: mysql -h主机地址 ...
- 为何PHP插入mysql的中文是乱码?【坑】
依然没有找到最终的解决方法,PHP插入的中文在phpmyadmin中看是乱码,但是用PHP获取之后显示正常: 可以在phpmyadmin中直接插入中文,在PHPmyadmin中显示正常,用PHP获取中 ...
- flask-admin章节三:数据库迁移工具 alembic初步使用
1. 概述 基于flask框架构建web,一般会使用sqlchemy(在flask中使用sqlchemy可以参考这里)作为数据库引擎. 这样业务的逻辑就可以做到不跟具体的数据库类型相耦合,具体后端业务 ...
- 激活神器 KMSAuto Net 2015 v1.3.8
KMSAuto Net – Windows 操作系统 KMS 自动激活工具!支持 Windows Vista,7,8,8.1,10, Server 2008,2008 R2,2012,2012 R2, ...
- mongo基本操作
创建数据库文件的存放位置,比如d:/mongodb/data/db.启动mongodb服务之前需要必须创建数据库文件的存放文件夹,否则命令不会自动创建,而且不能启动成功. 打开cmd(windows键 ...
- CString 字符串截取_函数
CString res;CString str = _T("abcdefghijklmn"); res = str.Mid(2,3); //从第3位字母开始,共取3个字符ASSE ...
- VMware克隆SUSE网卡配置
1.配置DNS vim /etc/resolv.conf domain sitenameserver 202.98.0.682.配置网关 vim /etc/sysconfig/network/rout ...
- select值的获取及修改
例子: <select id="a" name="a"> <options value="1">a</opti ...
- sharepoint 网站创建
打开开始菜单,右键sharepoint管理中心以管理员身份打开 打开管理web应用程序菜单,并新建web应用程序 新建web应用程序配置,大多数采用默认配置. 建议自定义端口号,URL的端口号和设置的 ...
- JS命名空间实例
var types = new MeetingList.EventList(msg); $(".divclass").html(types.Build(new Date($(&qu ...