题目链接: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. [Bzoj4289]PA2012 Tax(Dijkstra+技巧建图)

    Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...

  2. Java实现Avl树

    Avl树即左右子树的深度[高度]相差不可超过1,所以在插入key的时候,就会出现需要旋转[更改根节点]的操作 下面是源代码: /* the define of avltree's node */ cl ...

  3. NodeJS解析客户端请求的body中的内容

    request.body undefinded 解决方法 app.use(bodyParser.json({extended: false}));app.use(bodyParser.urlencod ...

  4. Android面试收集录12 View测量、布局及绘制原理

    一.View绘制的流程框架 View的绘制是从上往下一层层迭代下来的.DecorView-->ViewGroup(--->ViewGroup)-->View ,按照这个流程从上往下, ...

  5. web.py上传文件并解压

    有个需求是从php端上传zip文件到python端并且解压到指定目录,以下是解决方法 1.python端,使用的web.py def POST(self): post_data = web.input ...

  6. MySQL 5.7.18 压缩包版配置记录

    1.解压到一个目录(建议根目录),比如:D:\mysql2.在系统Path中添加 D:\mysql\bin3.这个版本不带my-default.ini,需要自己写,放在D:\mysql\my.ini, ...

  7. 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目7

    2014-04-24 22:06 题目:搜索引擎问题,如果有一列100台服务器的集群,用来响应查询请求,你要如何设计query分发和cache策略? 解法:query分发可以用计算数字签名并对机器数取 ...

  8. 为什么要搞vim

    一. 先得想清楚折腾vim受的这顿折磨值不值.值.零碎记录几点. 迫使我使用vim的原因如下: (1)之前实习的公司的开发机上只有vim,以后工作的公司也只有vim,同部门的同事大都用vim:如果不用 ...

  9. 【Insert Interval】cpp

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  10. 深copy和浅copy

    浅copy:其实就是将容器中的内存地址存放进另一个容器中,所以两个容器本身的内存地址不相同,但容器里面的内存地址相同 代码如下: 深copy:就是从里到外完完全全复制了所有值,存进另外的内存空间,并赋 ...