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()的更多相关文章

  1. LeetCode 028 Implement strStr()

    题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...

  2. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  3. 【LeetCode】028. Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. [LeetCode] 28. Implement strStr() 实现strStr()函数

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. Leetcode 28——Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  7. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  8. 【leetcode】Implement strStr() (easy)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. 【leetcode】Implement strStr()

    Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...

随机推荐

  1. BZOJ-1196 公路修建问题 最小生成树Kruskal+(二分??)

    题目中一句话,最大费用最小,这么明显的二分的提示(by 以前morestep学长的经验传授)...但完全没二分,1A后感觉很虚.. 1196: [HNOI2006]公路修建问题 Time Limit: ...

  2. (Beta)Let's-Beta阶段展示博客

    康家华:http://www.cnblogs.com/AmazingMax/ 马阿姨:http://www.cnblogs.com/oushihuahua/ 刘彦熙:http://www.cnblog ...

  3. Ubuntu添加开机自动启动程序的方法

    文章出处:http://hi.baidu.com/gcc_gun/blog/item/fe9bbc4b84e911fa83025cb8.html 1. 开机启动时自动运行程序 Linux加载后, 它将 ...

  4. PHP 7.1 新特性一览

    可空类型主要用于参数类型声明和函数返回值声明.主要的两种形式如下:<?phpfunction answer(): ?int  {   return null; //ok}function ans ...

  5. hiho1015(kmp+统计出现次数)

    http://hihocoder.com/problemset/problem/1015 时隔多天再次温习了一下KMP #include <iostream> #include <c ...

  6. P1049送给圣诞夜的礼品(矩阵十大问题之四)

    https://vijos.org/p/1049 P1049送给圣诞夜的礼品 Accepted 标签:组合数学送给圣诞夜的礼物[显示标签]     返回代码界面 | 关闭   Pascal Pasca ...

  7. iOS 刚刚,几分钟前,几小时前,几天前,几月前,几年前

    - (NSString *)compareCurrentTime:(NSDate*) compareDate { NSTimeInterval timeInterval = [compareDate ...

  8. MySQL Cluster(MySQL 集群) 初试(转)

    作/译者:叶金荣(imysql#imysql.com>),来源:http://imysql.com,欢迎转载. 作/译者:叶金荣(Email: ),来源:http://imysql.cn,转载请 ...

  9. webservice和restful的区别

    REST是一种架构风格,其核心是面向资源,REST专门针对网络应用设计和开发方式,以降低开发的复杂性,提高系统的可伸缩性.REST提出设计概念和准则为: 1.网络上的所有事物都可以被抽象为资源(res ...

  10. java连接各种数据库代码大全

    1.Oracle8/8i/9i数据库(thin模式)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();S ...