Java for LeetCode 028 Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
解题思路一:
暴力枚举,JAVA实现如下:
static public int strStr(String haystack, String needle) {
for(int i=0;i<=haystack.length()-needle.length();i++)
if(haystack.substring(i, i+needle.length()).equals(needle))
return i;
return -1;
}
解题思路二:
经典的KMP算法
参考链接:
http://kb.cnblogs.com/page/176818/
http://blog.csdn.net/zxasqwedc/article/details/41956381
JAVA 实现
static public int strStr(String haystack, String needle) {
int[] next = new int[needle.length()+1];
int i = 0, j = -1;
//i可以代表索引,其实也可以理解为一个间隔,j代表前面已匹配的
//next[1,next.length-1] 恰好表示部分匹配值
next[0] = -1;
while (i < needle.length()) {
if (j == -1 || needle.charAt(i) == needle.charAt(j)) {
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
i = 0;
j = 0;
while (i < haystack.length()) {
if (j == -1 || haystack.charAt(i) == needle.charAt(j)) {
i++;
j++;
} else
j = next[j];//是next[j]不是next[j-1],算法核心
if (j == needle.length())
return i - needle.length();
}
return -1;
}
Java for LeetCode 028 Implement strStr()的更多相关文章
- LeetCode 028 Implement strStr()
题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- 【LeetCode】028. Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [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()
Implement strStr(). Return 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() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
随机推荐
- Solr -- 实时搜索
在solr中,实时搜索有3种方案 ①soft commit,这其实是近实时搜索,不能完全实时. ②RealTimeGet,这是实时,但只支持根据文档ID的查询. ③和第一种类似,只是触发softcom ...
- groovy-正则表达式
Groovy使用~”pattern” 来支持正则表达式,它将使用给定的模式字符串创建一个编译好的Java Pattern 对象.Groovy也支持 =~(创建一个Matcher)和 ==~ (返回bo ...
- eclipse中运行python脚本中有注释为中文的内容,报错:SyntaxError: Non-ASCII character '\xe5'
'''Created on 2015年7月2日 @author: liujuan'''import sysreload(sys) 以上为注释的有个日期中文的,结果运行报错:SyntaxError: N ...
- 透透彻彻IoC(你没有理由不懂!)
http://www.myexception.cn/open-source/418322.html 引述:IoC(控制反转:Inverse of Control)是Spring容器的内核,AOP.声明 ...
- Java初学(一)
一.初识Java 1.JVM:Java跨平台是基于JVM(Java虚拟机)的,JVM不是跨平台的,针对不同平台有对应的JVM软件 2.JRE:Java开发出来的软件如果要运行还需要在环境中安装JRE( ...
- 也谈闭包--小白的JS进阶之路
JavaScript当然是会用的,不过没有深入系统的学习罢了.平常还是用JQuery比较多,原生的JS用到的很少. 不过前端时间学习Ruby,被动态语言的强大和魔幻给震惊了一把.了解Ruby后,我把目 ...
- MySQL中varchar类型在5.0.3后的变化
1.mysql varchar类型变化:mysql 5.0.3 之前: 0--255字节 varchar(20)中的20表示字节数,如果存放urf8编码的话只能放6个汉字. MySQL 5.0.3 之 ...
- memcache的内存回收机制
memcache不会释放内存,而是重新利用. 在缓存的清除方面,memcache是不释放已分配内存.当已分配的内存所在的记录失效后,这段以往的内存空间,memcache只会重复利用. memcache ...
- mybatis 使用resultMap实现关联数据的查询(association 和collection )
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...
- [Angularjs]asp.net mvc+angularjs+web api单页应用
写在前面 最近的工作一直在弄一些h5的单页应用,然后嵌入到app的webview中.之前一直在用angularjs+html+ashx的一套东西.实在是玩腻了.然后就尝试通过asp.net mvc的方 ...