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 ...
随机推荐
- SAS学习笔记31 SAS随机分组方法及实现
随机分组方法包括: 简单随机化(simple randomization) 区组随机化(block randomization) 分层随机化(stratified randomization) 分层区 ...
- 深度剖析Kubernetes API Server三部曲 - part 2
欢迎来到深入学习Kubernetes API Server的系列文章的第二部分.在上一部分中我们对APIserver总体,相关术语及request请求流进行探讨说明.在本部分文章中,我们主要聚焦于探究 ...
- JDBC 学习复习6 学习与编写数据库连接池
之前的工具类DBUtil暴露的问题 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大的 ...
- 【es6】将2个数组合并为一个数组
//第一种 一个数组中的值为key 一个数组中的值为value let arr1 = ['内存','颜色','尺寸']; let arr2 = [1,2,3]; let temp = arr1.map ...
- PowerBulider获取计算机mac地址
PowerBulider获取计算机mac地址 1.下载GETNET.DLL获取网络资源的API 2.PB的全局函数中的引入需要API,常用API列表如下 //得到计算机名字 function bool ...
- java利用MultipartRequest的getFileName方法不能得到原文件名问题
想利用MultipartRequest的getFileName方法来一次获取多个上传的文件名字时,得到的不是文件的名字,而是 input 的name属性 最后找到了答案,解决方法,参照http://s ...
- Ubuntu 远程管理常用命令
目标 关机/重启 shutdown 查看或配置网卡信息 ifconfig ping 远程登录和复制文件 ssh scp 01. 关机/重启 序号 命令 对应英文 作用 01 shutdown 选项 时 ...
- 第三章 Django之动态网页基础(1)
前一章中,我们解释了如何建立一个 Django 项目并启动 Django 开发服务器.当然,那个网站实际并没有干什么有用的事情,它所做的只是显示 It worked!消息.让我们来做些改变.本章将介绍 ...
- TightVNC安装
软件版本:tightvnc-2.8.8-gpl-setup-64bit.msi 后面一路默认,虚拟机端口默认从5900开始递增.
- GNS3
什么是GNS? GNS Graphical Network Simulator Simulator or Emulator? 尽管GNS全拼包含simulator,但实际上是emulator.我们说其 ...