题目链接: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. python sys模块和序列化模块

    sys模块是与python解释器交互的一个接口: sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0),错误退出sys.exit( ...

  2. POJ 3580 SuperMemo 伸展树

    题意: 维护一个序列,支持如下几种操作: ADD x y D:将区间\([x,y]\)的数加上\(D\) REVERSE x y:翻转区间\([x,y]\) REVOLVE x y T:将区间\([x ...

  3. Django的Field(字段)

    字段 1.models.AutoField 自增列 = int(11) 如果没有的话,默认会生成一个名称为 id 的列,如果要显示的自定义一个自增列,必须将给列设置为主键 primary_key=Tr ...

  4. USACO Section2.2 Preface Numbering 解题报告 【icedream61】

    preface解题报告----------------------------------------------------------------------------------------- ...

  5. android压力测试monkey简单使用

    monkey是android sdk自带的压力测试工具,简单使用如下: 用adb shell进入adb shell环境后,用下面命令进行测试 monkey -p com.xxx.yyy -v num ...

  6. 玩转Node.js(一)

    玩转Node.js(一) 在说Node.js之前,我们先来说说js,如果你也曾开发过前端,那么你一定接触到了这个叫JavaScript有趣的东西,而对于JavaScript,你只会基本的操作——为we ...

  7. python作业:模拟登陆(第一周)

    模拟登陆作业需求: 1. 用户输入帐号密码进行登陆 2. 用户信息保存在文件内 3. 用户密码输入错误三次后锁定用户 额外实现功能: 1.提示输入错误次数 2.输入已锁定用户会提示 3.用户不存在会提 ...

  8. .net网站数据抓取

    最新项目需要抓取人民币汇率中间价的数据,所以就写了个简单的爬虫抓取数据.抓取的网站为:http://www.safe.gov.cn/wps/portal/sy/tjsj_hlzjj_inquire # ...

  9. Redux & React & react-redux

    Redux Redux & React & react-redux https://redux.js.org/ https://redux.js.org/api https://red ...

  10. RabbitMQ vhost 配置

    RabbitMQ vhost 配置 rabbitmqctl set_vhost_limits是用来定义虚拟主机限制的命令 配置最大连接限制 要限制vhost vhost_name中并发客户端连接的 总 ...