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. 获取和设置tinyMCE 4编辑器的内容

    对于tinymce编辑器是无法通过js进行内容的读写的,必须使用编辑器自身的方法才行,下面是一些方法,希望能对用到的朋友有所帮助: 1.如果当前页面只有一个编辑器: 获取内容:tinyMCE.acti ...

  2. 修改mysql最大连接数的方法

    MYSQL数据库安装完成后,默认最大连接数是100,一般流量稍微大一点的论坛或网站这个连接数是远远不够的,增加默认MYSQL连接数的方法有两个 方法一:进入MYSQL安装目录 打开MYSQL配置文件 ...

  3. Windows python3.3下安装BeautifulSoup

    首先在官网下载:http://www.crummy.com/software/BeautifulSoup/#Download BeautifulSoup在版本4以上都开始支持python3了,所以就下 ...

  4. GMM算法k-means算法的比较

    1.EM算法 GMM算法是EM算法族的一个具体例子. EM算法解决的问题是:要对数据进行聚类,假定数据服从杂合的几个概率分布,分布的具体参数未知,涉及到的随机变量有两组,其中一组可观测另一组不可观测. ...

  5. 关于bash的shellshock漏洞

    这一漏洞的描述如下: Shellshock (CVE-2014-6271, CVE-2014-6277, CVE-2014-6278, CVE-2014-7169, CVE-2014-7186, CV ...

  6. C++ STL之stack

    因为总用vector,却忘记了有stack,今天用到栈顶的值才想起来,说起来stack很方便,也很容易用,看下边例子: #include<stack> #include<iostre ...

  7. html标签页图标

    在head标签加入如下内容: <!--可以在收藏夹中显示出图标--> <link rel="Bookmark" type="image/png" ...

  8. Glut 回调函数小结

    2014-04-08  16:25:50   void glutDisplayFunc(void (*func)(void)); 注册当前窗口的显示回调函数 参数: func:形为void func( ...

  9. Protocol Buffers(Protobuf)开发者指南---概览

    Protocol Buffers(Protobuf)开发者指南---概览 欢迎来到protocol buffers的开发者指南文档,protocol buffers是一个与编程语言无关‘.系统平台无关 ...

  10. Practical Machine Learning For The Uninitiated

    Practical Machine Learning For The Uninitiated Last fall when I took on ShippingEasy's machine learn ...