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. 【CodeForces 577B】Modulo Sum

    题 题意 给你n(1 ≤ n ≤ 106)个数a1..an(0 ≤ ai ≤ 109),再给你m( 2 ≤ m ≤ 103)如果n个数的子集的和可以被m整除,则输出YES,否则NO. 分析 分两种情况 ...

  2. BZOJ-1003 物流运输trans SPFA+DP

    傻逼错误耗我1h,没给全范围坑我1A.... 1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec Memory Limit: 162 MB Submit: 529 ...

  3. 【bzoj4010】 HNOI2015—菜肴制作

    http://www.lydsy.com/JudgeOnline/problem.php?id=4010 (题目链接) 题意 给出一张无向图要求出一个拓扑序列满足1的位置最靠前 ,在保证上面的条件下使 ...

  4. 大数据测试之hadoop命令大全

    1.列出所有Hadoop Shell支持的命令 $ bin/hadoop fs -help2.显示关于某个命令的详细信息 $ bin/hadoop fs -help command-name3.用户可 ...

  5. POJ2392Space Elevator(贪心+背包)

    Space Elevator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9970   Accepted: 4738 De ...

  6. linux 破解版confluence安装

    OS  centos 6.5  需要的安装包如下: jre-7u67-linux-x64.rpm atlassian-confluence-5.4.4-x64.bin mysql-connector- ...

  7. leach协议matlab仿真代码

    http://www.ilovematlab.cn/thread-177006-1-1.html LEACH協議clear;%清除內存變量 xm=100;%x軸範圍ym=100;%y軸範圍 sink. ...

  8. Visual Studio IIS Express 不支持.json后缀的mime类型

    vs默认web调试工具中并不支持.json后缀的mime类型. 如何解决,两个方案: 局部单应用:web.config: <system.webServer> .... <stati ...

  9. Linux下安装php screw

    1.下载地址:http://sourceforge.net/projects/php-screw/ 2. tar zxvf php_screw_1.5.tar.gz cd php_screw_1.5 ...

  10. c语言中的fgets函数

    fgets()函数用于从文件流中读取一行或指定个数的字符,其原型为: char * fgets(char * string, int size, FILE * stream); 参数说明:string ...