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 ...
随机推荐
- @Adaptive注解
关于@Adaptive注解 引用dubbo官方文档的一段话: Adaptive 可注解在类或方法上.当 Adaptive 注解在类上时,Dubbo 不会为该类生成代理类.注解在方法(接口方法)上时 ...
- docker相关--dockerd日志设置
背景 线上容器dockerd的后台程序打印了超过几十G的日志 Docker daemon日志的位置: Docker daemon日志的位置,根据系统不同各不相同. Ubuntu - /var/log/ ...
- Admui相关第三方插件
ace 版本:1.2.3au 官网:https://github.com/ajaxorg/ace-builds/ 许可:BSD 依赖:无 DataAPI:data-pulgin="ace&q ...
- css border 三角形阴影(不规则图形阴影) & 多重边框的制作
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! border 的组合写法 border:border-width border-style border- ...
- (十五)struts2之注解
一.作用 以用来替换struts.xml配置文件 使用前提 :必须引入struts2-convention-plugin-2.3.14.jar 这个jar包 二.参数 @Action来代替<ac ...
- (四)自定义多个Realm以及Authenticator与AuthenticationStrategy
多Realm配置 #声明一个realm myRealm1=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1 myRealm2=com.githu ...
- 获取类的描述信息 DescriptionAttribute
static void Main(string[] args) { var attrs = typeof(TestClass).GetCustomAttributes(typeof(System.Co ...
- window.open打开一个新空白页面,不会自动刷新【解决方案】
调用js方法: function BuildPostForm(fm, url, target) { var e = null, el = []; if (!fm || !url) return e; ...
- jmeter分布式压力测试配置操作
前提准备条件:1.主控机一台为master,ip地址:10.8.88.1772.负载机一台为slave, ip地址:10.8.88.1193.主控机和负载机都安装一样的JDK环境和jmeter版本.5 ...
- 阿里Java架构师打包 FatJar 方法小结
在函数计算(Aliyun FC)中发布一个 Java 函数,往往需要将函数打包成一个 all-in-one 的 zip 包或者 jar 包.Java 中这种打包 all-in-one 的技术常称之为 ...