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 ...
随机推荐
- 在Yii2中集成Markdown编辑器
安装命令: composer require ijackua/yii2-lepture-markdown-editor-widget:dev-master 可能会遇到的问题 如果在下载依赖包的过程中出 ...
- 编写并提取通用 ShellCode
简易 ShellCode 虽然可以正常被执行,但是还存在很多的问题,因为上次所编写的 ShellCode 采用了硬编址的方式来调用相应API函数的,那么就会存在一个很大的缺陷,如果操作系统的版本不统一 ...
- 2019杭电多校二 L. Longest Subarray (线段树)
大意: 给定序列$a$, 元素范围$[1,C]$, 求一个最长子序列, 满足每个元素要么不出现, 要么出现次数$\le K$. 枚举右端点, 考虑左端点合法的位置. 显然一定是$C$种颜色合法位置的交 ...
- Codeforces 666E Forensic Examination(广义后缀自动机+线段树合并)
将所有串(包括S)放一块建SAM.对于询问,倍增定位出该子串所在节点,然后要查询的就是该子串在区间内的哪个字符串出现最多.可以线段树合并求出该节点在每个字符串中的出现次数. #include<b ...
- php底层变量存储
变量存储 php的变量使用一个结构体 zval来保存的,在Zend/zend.h中我们可以看到zval的定义 struct _zval_struct { /* Variable information ...
- CMake入门-01-从HelloWorld开始
工作环境 系统:macOS Mojave 10.14.6 CMake: Version 3.15.0-rc4 从 Hello,World! 开始 (1) 新建 hello 目录,创建文件 CMakeL ...
- ES6--JavaScript扩展知识点(let、const、解构)
一,ES2015(ES6)新增了两个声明变量的关键字:let.const let:只在代码块内{}有效,不可重复声明,不会提前初始化 1.只在代码块内有效 { let a = 1; var b = 2 ...
- undefined reference to `udev_device_get_action'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libusb-1.0.a(libusb_1_0_la-linux_udev.o): In ...
- 3.Hbase数据模型
3.1.Hbase数据模型: 概念视图: 物理视图 Hbase数据在存储系统中是以列族来体现的[Column Family],任何时候可以随意的添加一列到已经存在的列族中 空的单元格在表中不做存储也不 ...
- linux配置全局环境变量-jdk
1.vi /etc/profile 2.输入大写G,定位内容末尾. 3.在末尾输入 export JAVA_HOME=/home/order/soft/jdk PATH=$PATH:$HOME/.lo ...