问题描述

实现 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. lvm 扩容

    总体思路: 逻辑卷要扩容,先扩容对应卷组, 扩容卷组的方式: 添加新的物理卷(磁盘已有分区,扩容后新建分区:或者新加了一块硬盘创建了新的物理卷),vgextend myvg /dev/vdb 扩容,/ ...

  2. [Elasticsearch] ES 的Mapping 设计在实际场景中应用

    背景 项目中有个需求是需要几个字段作为标签,统计各个标签的文档数量,同时支持分词后的全文检索功能. 原有的mapping设计: curl -XPUT http://ip:9200/meta_es_me ...

  3. c++之别让异常逃离析构函数

    关于 本文代码演示环境: VS2017. 代码写的够不规范,目的是为了缩短文章篇幅. 本文主要是为了加深印象,写了好多次的代码,还是忘记了这茬.... 之前上传到github的代码会慢慢改过来. 本文 ...

  4. Python 图片转字符画 学习笔记

    Python 图片转字符画 学习笔记 标签(空格分隔): Python 声明:此文章和所有代码是学习笔记,非原创,原文教程地址:https://www.shiyanlou.com/courses/37 ...

  5. 【剑指Offer】06. 从尾到头打印链表 解题报告(Java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈 递归 数组 日期 题目地址:https://leetcode ...

  6. 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)

    [LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  7. Codeforces 888E:Maximum Subsequence(枚举,二分)

    You are given an array a consisting of n integers, and additionally an integer m. You have to choose ...

  8. CapstoneCS5212|DP to VGA|CS5212设计电路方案

    CS5212功能概述 CS5212是一款DisplayPort端口到VGA转换器,它结合了DisplayPort输入接口和模拟RGB DAC输出接口.嵌入式单片机基于工业标准8051核心. CS521 ...

  9. 论文翻译:2020_Acoustic Echo Cancellation by Combining Adaptive Digital Filter and Recurrent Neural Network

    论文地址:https://arxiv.53yu.com/abs/2005.09237 自适应数字滤波与循环神经网络相结合的回声消除技术 摘要 回声消除(AEC)在语音交互中起关键作用.由于明确的数学原 ...

  10. Java初学者作业——分析计费规则后,编写程序输入乘坐出租车的时间和里程数,计算里程价格

    返回本章节 返回作业目录 需求说明: 某城市的出租车计费规则如下: 在 7:00 - 23:00 之间,3km 以内收取起步价 10 元,超过 3km 每 km 收取 2 元. 如果不在这个时间段,在 ...