leetcode 28. 实现 strStr()
问题描述
实现 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 OJ> -LeetCode 28. 实现 strStr()
题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...
- 前端与算法 leetcode 28.实现 strStr()
# 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...
- Java实现 LeetCode 28 实现strStr()
28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode 28 Implement strStr() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 28——Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- Excel里的格式会自动变成日期或会计专用吗?(Excel技巧集团)
Excel里的格式会自动变成日期或会计专用? 正常情况下当然不会了,可是最近却有很多很多同学问这样的问题,并把这个问题列成了Excel2007和2010的一个Bug,可是小妖同学却从来没遇到过这样的问 ...
- 基于nginx的rtmp直播服务器(nginx-rtmp-module实现)
首先,在搭建服务之前先了解下目前主流的几个直播协议: 1.RTMP: 实时消息传输协议,Real Time Messaging Protocol,是 Adobe Systems 公司为 Flash 播 ...
- c++ 设计模式概述之享元
类写的不够规范,目的是为了缩短篇幅,实际中其不要这样做. 参考文章: 1. http://c.biancheng.net/view/1371.html 1.概述 A.享元,我的理解是: 共享的模块单元 ...
- 【LeetCode】695. Max Area of Island 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 【LeetCode】809. Expressive Words 解题报告(Python)
[LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- Fast Matrix Operations(UVA)11992
UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...
- centos 各版本下载
地址: go to http://vault.centos.org/ for packages.
- RabbitMQ学习笔记五:RabbitMQ之优先级消息队列
RabbitMQ优先级队列注意点: 1.只有当消费者不足,不能及时进行消费的情况下,优先级队列才会生效 2.RabbitMQ3.5以后才支持优先级队列 代码在博客:RabbitMQ学习笔记三:Java ...
- uniapp以及微信小程序中scroll-view隐藏滚动条 自定义滚动条
隐藏滚动条 1.全局隐藏滚动条,在app.vue中 ::-webkit-scrollbar{ display: none; } 2.局部隐藏藏滚动条 样式没有使用scoped属性时, 否则无效. .u ...
- JSP中使用<c:forEach>标签循环遍历元素
1.forEach标签元素 <c:forEach items="接收集合对象" var="迭代参数名称" varStatus="迭代状态,可访问 ...