问题描述

实现 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. LuoguP7071 [CSP-J2020] 优秀的拆分 题解

    Content 给定一个数 \(n\),求是否能够拆分成 \(2\) 的正整数次幂的和的形式,并给出具体方案. 数据范围:\(1\leqslant n\leqslant 10^7\). Solutio ...

  2. iframe父子页面js之间的调用

    父级页面:mian.html 子页面1:top.html 子页面2:index.html 页面关系 <div onclick="_top()">调用contentTop ...

  3. JAVA 通过url下载图片保存到本地

    //java 通过url下载图片保存到本地 public static void download(String urlString, int i) throws Exception { // 构造U ...

  4. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  5. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  6. J. Bottles

    J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...

  7. 【LeetCode】807. Max Increase to Keep City Skyline 解题报告(Python &C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【C++】关于new分配空间

    1如果不使用new,则在函数结束时内存被回收,指针变成野指针 #include <iostream> using namespace std; struct Node { int val; ...

  9. CSS实现鼠标移入时图片的放大效果以及缓慢过渡

    transform:scale()可以实现按比例放大或者缩小功能. transition可以设置动画执行的时间,实现缓慢或者快速的执行动画,效果图如下: 源码: <!DOCTYPE html&g ...

  10. Adaptive gradient descent without descent

    目录 概 主要内容 算法1 AdGD 定理1 ADGD-L 算法2 定理2 算法3 ADGD-accel 算法4 Adaptive SGD 定理4 代码 Malitsky Y, Mishchenko ...