leetcode解题报告(9):Implement strStr()
描述
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
分析
设置一个索引index,初始化为0,用于表示needle的下标。遍历haystack,如果发现haystack的当前元素和needle中下标为index的元素相等,就将index加1;若index已等于needle的长度,说明已经遍历完needle,找到了这个子串。这时候返回的值要为haystack中needle的第一个元素所在的位置,如,对于两个字符串a和b:
string a("aasssa");
string b("sa");
当找到这个子串的时候,对于a来说,它的下标i已经是5了,而要返回的值为4,即指向"sa"中的s的下标。此时对于b来说,其index的值为2,即它的长度。不失一般性,返回的值应为 i-index+1 。
若遍历完haystack时index仍不为needle的长度,说明没有找到子串,因此返回-1。
另外两个比较奇葩的边界情况是需要单独考虑的,第一个是:
string haystack("a");
string needle("");
这时候返回的居然是0而不是-1!
经测试,发现needle[0]输出的值为'a',和haystack的第一个元素刚好相等,因此返回0.而事实上这时候needle的长度是为0的。这是我无法理解的。
另外一个情况和上面的类似:
string haystack("");
string needle("");
这时候返回的也是0。
代码如下:
class Solution {
public:
int strStr(string haystack, string needle) {
int index = 0;
if((haystack[0] == 'a' && needle.size() == 0)
|| (haystack.size() == 0 && needle.size() == 0))return 0;
for(int i = 0; i != haystack.size(); ++i){
if(haystack[i] == needle[index]){
++index;
if(index == needle.size())return i - index + 1;
}else{
i -= index;
index = 0;
}
}
return -1;
}
};
leetcode解题报告(9):Implement strStr()的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- leetcode第27题--Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- 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(). Returns the index of the first occurrence of needle in haystack, or -1 if nee ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
随机推荐
- GPIO 输出—使用固件库点亮 LED
编程要点 1. 使能 GPIO 端口时钟: 2. 初始化 GPIO 目标引脚为推挽输出模式: 3. 编写简单测试程序,控制 GPIO 引脚输出高.低电平. LED的电路图 过程: 1.拷贝一个库函 ...
- hadoop2.7.7 分布式集群安装与配置
环境准备 服务器四台: 系统信息 角色 hostname IP地址 Centos7.4 Mster hadoop-master-001 10.0.15.100 Centos7.4 Slave hado ...
- Qt中的常用容器类(解释比较全面,有插图)
在Qt库中为我们提供了一系列的基于模板的容器类.这些类可以被用来存储特定类型的项.例如,如果你需要一个大小可以变得QString数组,那么可以使用QVector<QString>. 这些容 ...
- archive_lag_target参数
需求,由于一套生产环境归档日志切换频率过低,建议修改参数,使其间隔一定时间周期自动切换生成归档日志; SQL>; THREAD# SEQUENCE# TO_CHAR(COMPLETION_TIM ...
- dg环境连接ORA-00604,ORA-16000: database open for read-only access
报错信息 根据客户提供的报错信息, ORA-: error occurred at recursive SQL level ORA-: database open for read-only acce ...
- dockerfile相关命令
官方dockerfile:https://github.com/play-with-docker/play-with-docker 可以根据一直的镜像,学习dockerfile的编写 dockerfi ...
- Spring AOP编程经验总结
编程范式概览:面向过程,面向对象,函数式编程,事件驱动编程,面向切面等, AOP是什么? Spring AOP是采用面向切面编程的编程范式,而非编程语言,它只能解决特定问题,而非所有问题,它与OOP不 ...
- 关于微信小程序获取view的动态高度填坑
wx.createSelectorQuery().select('#box').boundingClientRect(function (rect) { width = rect.width heig ...
- node express4 + 前端自动刷新
官网快速生成:http://www.expressjs.com.cn/starter/generator.html 1.安装 express 1.应用生成器工具 express-generator ...
- scss语法格式(常用版记录)
这篇文章是我自己在学习Scss时的笔记~ 更多学习可以参照官网(链接:https://www.sass.hk/docs/) 一,Scss语法格式 1.嵌套规则 2.父选择器&(伪类嵌套 ...