题目链接: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. 使wlr写cnblog的博客-2 设置cnblog帐号

    ref:http://www.cnblogs.com/liuxianan/archive/2013/04/13/3018732.html   使用: 打开Windows Live Writer,第一次 ...

  2. NumPy库入门

    ndarray数组的元素类型 ndarray数组的创建 ndarray数组的操作 ndarray数组的运算

  3. 教你一步学会安装Hue

    一.简介 hue是一个开源的apache hadoop ui系统,由cloudear desktop演化而来,最后cloudera公司将其贡献给了apache基金会的hadoop社区,它基于pytho ...

  4. Python数据类型三

    一.帮助 如果想知道一个对象(object)更多的信息,那么可以调用help(object)!另外还有一些有用的方法,dir(object)会显示该对象的大部分相关属性名,还有object._ doc ...

  5. centos使用--排查服务是否可用

    端口与服务的关系 一台拥有IP地址的主机可以提供许多服务,比如Web服务.FTP服务.SMTP服务等,这些服务完全通过1个IP地址来实现.那么,主机是怎样区分不同的网络服务呢?显然不能只靠IP地址,因 ...

  6. QBASIC教程

    Qbasic 程序设计入门 BASIC(Beginner’s All-purpose Symbolic Instruction Code 的缩写,意为初学者通用符号指令代码)语言是在1964年由美国的 ...

  7. NGUI注册事件的三种方式

    1.第一种方式 当一个元素要执行某个方法,而这个方法在此元素赋予的脚本上有,那么直接会调用此方法,但此方法的名称必须是内置的固定名称,例如OnClick,OnMouseOver,OnMouseOut等 ...

  8. leetcode 179. 最大数 解题报告

    给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数. 示例 1: 输入: [10,2] 输出: 210 示例 2: 输入: [3,30,34,5,9] 输出: 9534330 说明: 输出结果 ...

  9. linux 出错 “INFO: task xxxxxx: 634 blocked for more than 120 seconds.”的3种解决方案(转)

    linux 出错 “INFO: task xxxxxx: 634 blocked for more than 120 seconds.”的3种解决方案 1 问题描述 服务器内存满了,ssh登录失败 , ...

  10. Android记事本11

    昨天: Activity的启动模式. 今天: 分析了一些网上的例子的源码. 遇到问题: 无.