题目链接: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. JQuery方法总结

    JQuery方法总结 Dom: Attribute:(属性) $("p").addClass(css中定义的样式类型); 给某个元素添加样式 $("img"). ...

  2. 《Cracking the Coding Interview》——第4章:树和图——题目6

    2014-03-19 04:16 题目:找出一棵二叉搜索树中的中序遍历后继节点,每个节点都有指针指向其父节点. 解法1:分两种情况:向下走时,先右后左:向上走时,先左后右.如果目标节点有右子树,就向右 ...

  3. Scrapy使用示例

    很多网站都提供了浏览者本地的天气信息,这些信息是如何获取到的呢,方法有很多种,大多是利用某些网站提供的天气api获取的,也有利用爬虫采集的.本文就介绍如何用Scrapy来采集天气信息(从新浪天气频道采 ...

  4. 我给女朋友讲编程CSS系列(4) CSS盒子模型

    什么是CSS盒子模型?如何学习CSS的盒子模型? 这篇文章,以 [分享 + 结论]  的方式来写. 1,  看w3school的[CSS 框模型概述] 网址为: http://www.w3school ...

  5. 玩转Openstack之Nova中的协同并发(二)

    玩转Openstack之Nova中的协同并发(二) 昨天介绍了Python中的并发处理,主要介绍了Eventlet,今天就接着谈谈Openstack中Nova对其的应用. eventlet 在nova ...

  6. sources-x.list

    deb http://debian.ustc.edu.cn/ubuntu/ xenial main multiverse restricted universe deb http://debian.u ...

  7. hnust 最小的x

    问题 G: 最小的x 时间限制: 1 Sec  内存限制: 128 MB提交: 2347  解决: 1155[提交][状态][讨论版] 题目描述 TSQ对DK进行地狱式训练,找出满足下面公式的最小的x ...

  8. 团队项目-第一次Scrum 会议

    时间:10.23 时长:30分钟 地点:F楼2层沙发休息处 工作情况 团队成员 已完成任务 待完成任务 解小锐 学习使用cocos creator 学习官方样例 陈鑫 学习JavaScript 学习c ...

  9. 哈夫曼树(C++优先队列的使用)

       给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称为哈夫曼树(Huffman Tree).哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近.    构造 假设有n个权 ...

  10. ComboBox列表自定义类保存数据

    之前没弄明白ComboBox还可以这样用. 先建一个ComboBox子项类,然后可以获取该项类做一些判断,关键是要重写ToString()方法. public class ComboItem { pu ...