题目链接:https://leetcode.com/problems/implement-strstr/description/

题目大意:字符串匹配,从字符串中,找到给定字符串第一次出现的位置下标,并返回。

法一:暴力,两个for循环,逐一比较每一个可能的字符串,一旦找到,则返回。代码如下(耗时508ms->10ms):

     public int strStr(String haystack, String needle) {
int res = -1, len_h = haystack.length(), len_n = needle.length();
if(len_n == 0) {
return 0;
}
//此处优化点:i <= len_h - len_n,当主字符串中字符个数已经过小时,则不需要再遍历了
for(int i = 0; i <= len_h - len_n; i++) {
//可能有当前字符串
if(len_n != 0 && haystack.charAt(i) == needle.charAt(0)) {
int k = i + 1, j = 1;
boolean flag = true;
for( ; j < len_n && k < len_h; j++) {
if(haystack.charAt(k++) != needle.charAt(j)) {
flag = false;
break;
}
}
if(flag == true && j == len_n) {
res = i;
break;
}
}
}
return res;
}

法二(借鉴):kmp,练习一下kmp。代码如下(耗时20ms):

     public int strStr(String haystack, String needle) {
int next[] = computeNext(needle);
int i = 0, j = 0, len_h = haystack.length(), len_n = needle.length();
for( ; i < len_h && j < len_n; i++) {
while(j > 0 && haystack.charAt(i) != needle.charAt(j)) {
j = next[j - 1];
}
if(haystack.charAt(i) == needle.charAt(j)) {
j++;
}
}
return (j == needle.length()) ? (i - j) : -1;
}
private int[] computeNext(String needle) {
int[] next = new int[needle.length()];
for(int i = 1, j = 0; i < needle.length(); i++) {
while(j > 0 && needle.charAt(i) != needle.charAt(j)) {
j = next[j - 1];
}
if(needle.charAt(i) == needle.charAt(j)) {
j++;
}
next[i] = j;
}
return next;
}

28.Implement strStr()---kmp的更多相关文章

  1. 28. Implement strStr()(KMP字符串匹配算法)

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

  2. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  3. 44. leetcode 28. Implement strStr()

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

  4. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...

  5. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

  6. Leetcode #28. Implement strStr()

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

  7. [LeetCode] 28. Implement strStr() 解题思路

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

  8. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

  9. 【LeetCode】28. Implement strStr() (2 solutions)

    Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...

随机推荐

  1. zeppelin之连接mysql

    上面的一篇文章,对于zeppelin的使用,只是我们对于数据存储在文件中,每一次对于当我们连接数据库的时候都会有问题,今天刚好 把这个问题解决今天我们刚好来介绍如何使用zeppelin来与数据进行连接 ...

  2. Toolbar中menu菜单文字颜色的修改

    Toolbar菜单中menu当中我们大多数都使用图片来按钮,可是有些时候我们也会直接使用文字,文字的颜色如何修改呢. 其实很简单,我们只要修改styles.xml文件中,添加一句 <item n ...

  3. 部署微软Nano Server的好处是什么?

    虚拟化对数据中心效率和硬件利用率产生了戏剧性的影响,但是接下来有关系统整合的主要责任落 在了操作系统的重量上.虚拟机通常运行企业级操作系统,比如Windows Server,但是Windows Ser ...

  4. 解决 ld: library not found for -lPods的问题

    现在打开有pods建好的workspace文件,尝试编译,会报ld: library not found for -lPods错误,原因就是工程里面的设置项覆盖了pods中xcconfig中的设置.解 ...

  5. 独立开发unity2d游戏的问答群

    129443731 有志独立开发游戏的,只讨论最新的unity2d技术的.群里面主要已问答为主,喜欢聊天的就别加群了,灌水多了会被t.希望能对unity2d比较了解的已及喜欢学习的人加入.

  6. Nuget的使用笔记-(使用nuget发布dll到www.nuget.org)

    Nuget是神马东东? 来自nuget.org官方的介绍 ----------------------------------------------------------------------- ...

  7. CS/BS架构的特点

    CS架构 优点: 1.有独立的客户端,安全性高 2.大部分业务都在客户端实现,可以实现很复杂的业务 缺点: 1.对环境要求高,需要安装客户端,推广速度慢 2.需要专门前后台的开发团队,维护成本高 B/ ...

  8. diskimage-builder

    Supported Distributions Distributions which are supported as a build host: Centos 6, 7 Debian 8 (“je ...

  9. shell之netstat命令

      语 法:netstat [-acCeFghilMnNoprstuvVwx] [-A<网络类型>][--ip] 补充说明:利用netstat指令可让你得知整个Linux系统的网络情况. ...

  10. 团队项目-第二次Scrum 会议

    时间:10.24 时长:30分钟 地点:线上 工作情况 团队成员 已完成任务 待完成任务 解小锐 学习官方样例 根据初步讨论结果编写初步的api文档 陈鑫 学习cocos creator基本使用 采用 ...