问题描述

实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。
示例 1:
输入: haystack = "hello", needle = "ll"
输出: 2
示例 2:
输入: haystack = "aaaaa", needle = "bba"
输出: -1
说明:
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

问题分析

  • 第一种方法:暴力方法,直接双层遍历,在最外层遍历只遍历到n-m+1而不是到n,因为needle的长度为m,后面肯定匹配不上。
  • 第二种方法:Sunday方法,通过记录一个偏移表,防止内层循环还要从头开始遍历。
    • Sunday算法对偏移量定义为:每个字符在串中最后出现的位置。
    • 对匹配规则是这样规定的:
      • 若匹配不成功:

        • 如果匹配串的后一个元素不在模式串中,那么只需要直接在后一个元素之后匹配即可,即游标移动一个模式串长度。
        • 如果匹配串的后一个元素在模式串中,则偏移到对齐位置,再进行比较。
      • 若匹配成功,则返回当前游标。

代码1

class Solution {
public:
int strStr(string haystack, string needle) {
if(needle.size() == 0) return 0;
int n = haystack.size();
int m = needle.size(),i,j,tmp;
if(n < m) return -1;
char c = needle[0];
for(i = 0; i < n-m+1; i++)
{
if(haystack[i] == c)
{
tmp = i;
for(j = 1; j < m;j++)
{
if(haystack[++tmp] != needle[j])
break;
}
if(j == m)return i;
}
}
return -1;
}
};

结果1

执行用时 :8 ms, 在所有 C++ 提交中击败了58.33%的用户
内存消耗 :9.2 MB, 在所有 C++ 提交中击败了32.48%的用户

代码2

class Solution {
public:
int strStr(string haystack, string needle) {
if(needle.size() == 0) return 0;
int n = haystack.size();
int m = needle.size(),i;
if(n < m) return -1;
map<char,int> table;
for(i = m-1; i >= 0; i--)//构造偏移表
if(table.find(needle[i]) == table.end())
table.insert(pair<char,int>(needle[i],m-i));
for(i = 0; i<n-m+1;)
{
if(haystack.substr(i,m) != needle)
{
if(table.find(haystack[i+m]) == table.end())
i = i + m + 1;//不配配
else
i = i + table.find(haystack[i+m])->second;
}
else
return i;
}
return -1;
}
};

结果2

执行用时 :4 ms, 在所有 C++ 提交中击败了93.65%的用户
内存消耗 :9.6 MB, 在所有 C++ 提交中击败了6.78%的用户

leetcode 28. 实现 strStr()的更多相关文章

  1. <每日 1 OJ> -LeetCode 28. 实现 strStr()

    题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...

  2. 前端与算法 leetcode 28.实现 strStr()

    # 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...

  3. Java实现 LeetCode 28 实现strStr()

    28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...

  4. 44. leetcode 28. Implement strStr()

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

  5. [LeetCode] 28. Implement strStr() 实现strStr()函数

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

  6. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

  7. Leetcode #28. Implement strStr()

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

  8. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

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

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

  10. Leetcode 28——Implement strStr()

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

随机推荐

  1. Excel里的格式会自动变成日期或会计专用吗?(Excel技巧集团)

    Excel里的格式会自动变成日期或会计专用? 正常情况下当然不会了,可是最近却有很多很多同学问这样的问题,并把这个问题列成了Excel2007和2010的一个Bug,可是小妖同学却从来没遇到过这样的问 ...

  2. 基于nginx的rtmp直播服务器(nginx-rtmp-module实现)

    首先,在搭建服务之前先了解下目前主流的几个直播协议: 1.RTMP: 实时消息传输协议,Real Time Messaging Protocol,是 Adobe Systems 公司为 Flash 播 ...

  3. c++ 设计模式概述之享元

    类写的不够规范,目的是为了缩短篇幅,实际中其不要这样做. 参考文章: 1. http://c.biancheng.net/view/1371.html 1.概述 A.享元,我的理解是: 共享的模块单元 ...

  4. 【LeetCode】695. Max Area of Island 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  5. 【LeetCode】809. Expressive Words 解题报告(Python)

    [LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  6. Fast Matrix Operations(UVA)11992

    UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...

  7. centos 各版本下载

    地址: go to http://vault.centos.org/ for packages.

  8. RabbitMQ学习笔记五:RabbitMQ之优先级消息队列

    RabbitMQ优先级队列注意点: 1.只有当消费者不足,不能及时进行消费的情况下,优先级队列才会生效 2.RabbitMQ3.5以后才支持优先级队列 代码在博客:RabbitMQ学习笔记三:Java ...

  9. uniapp以及微信小程序中scroll-view隐藏滚动条 自定义滚动条

    隐藏滚动条 1.全局隐藏滚动条,在app.vue中 ::-webkit-scrollbar{ display: none; } 2.局部隐藏藏滚动条 样式没有使用scoped属性时, 否则无效. .u ...

  10. JSP中使用<c:forEach>标签循环遍历元素

    1.forEach标签元素 <c:forEach items="接收集合对象" var="迭代参数名称" varStatus="迭代状态,可访问 ...