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. Teradata 语句简单优化

    Teradata 基本查询语言和SQL server 是一致的.有很小的区别. 功能没有SQL 全面,界面没有SQL 好看~ 1. teradata 里面经常会报一种错误: no enough spo ...

  2. TYVJ1864 守卫者的挑战

    P1864 [Poetize I]守卫者的挑战 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜 ...

  3. Mac OS X 10.9 Mavericks安装后,Xcode调试时模拟器黑屏的处理方法

    请耐心的等下去吧,少年! 装了Mac OS X 10.9 Mavericks的同学,如果碰到Xcode调试App时,模拟器黑屏(重置也无效),请耐心的等下去吧,大约10来分钟左右黑屏就会消失,App启 ...

  4. Eclipse在线安装ADT插件

    要想使用Eclipse开发Android应用,首先要安装一个ADT插件,在此记录一下在Eclipse中采用在线安装的方式ADT插件,我使用的Eclipse版本是:eclipse-jee-luna-SR ...

  5. 在tp中使用mongo数据库并建立连接的实例

  6. Java及Android开发环境搭建

    前言 自从接触java以来,配置环境变量折腾了好几次,也几次被搞得晕头转向,后来常常是上网查阅相关资料才解决.但是过一段时间后一些细节就会记不清了,当要在其他机子上配置时又得上网查或者查阅相关书籍,如 ...

  7. The Dirichlet Distribution 狄利克雷分布 (PRML 2.2.1)

    The Dirichlet Distribution 狄利克雷分布 (PRML 2.2.1) Dirichlet分布可以看做是分布之上的分布.如何理解这句话,我们可以先举个例子:假设我们有一个骰子,其 ...

  8. go tool proof

    echo list | go tool pprof -alloc_space gateway http://10.2.1.93:8421/debug/pprof/heap > abc.log e ...

  9. Struts2拦截器的应用

    拦截器类 public class AdminInterceptor extends AbstractInterceptor { private static final long serialVer ...

  10. 新浪微博客户端(13)-使用UIWebView加载OAuth授权界面

    使用UIWebView加载OAuth授权界面 DJOAuthViewController.m #import "DJOAuthViewController.h" @interfac ...