描述

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()的更多相关文章

  1. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  2. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  5. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. leetcode第27题--Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  7. LeetCode记录之28——Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. LeetCode(28)Implement strStr()

    题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if nee ...

  9. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

随机推荐

  1. APK反编译教程

    在学习Android开发的过程你,你往往会去借鉴别人的应用是怎么开发的,那些漂亮的动画和精致的布局可能会让你爱不释手,作为一个开发者,你可能会很想知道这些效果界面是怎么去实现的,这时,你便可以对改应用 ...

  2. 前端 aes 加密

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. (八)Struts标签基础(一)

    一.Struts标签分类 二.标签的使用 2.1 标签的主题 主题的设置与struts.xml中的常量<constant name="struts.ui.theme" val ...

  4. MVC模板页使用

    这里我们要做一个公共的模板,样式如下: 内容 ·asp.net mvc如何创建模板??1.在/Views/Shared/中右键-添加-视图 2.重命名为”HeadLayout”,勾选”创建为分部视图” ...

  5. win7电脑数字键盘失灵怎么办

    转自:https://zhidao.baidu.com/question/370674322729785324.html 解决方法: 第一步.同时按下“Windows键” + “R”调出运行窗口. 第 ...

  6. iOS 超级签名详解

    一.原理 把安装设备当做开发设备进行分发.说的明白一些,开发者可以在开发者后台添加手机的UDID,然后重新打包一个IPA文件,分发平台,然后被添加的UDID就可以下载. 二.优缺点 优势: 直接分发, ...

  7. HighChart中的tooltip的第一行数字明显比其他的字要小

    问题:HighChart中的tooltip的第一行数字明显比其他的字要小. 解决办法 headerFormat:'<span style="font-size: 14px;font-f ...

  8. 常用的virsh管理命令

    常用的virsh管理命令 列出所有的虚拟机 [root@ubuntu ~]# virsh list --all 显示虚拟机信息 [root@ubuntu ~]# virsh dominfo CentO ...

  9. 【leetcode】575. Distribute Candies

    原题 Given an integer array with even length, where different numbers in this array represent differen ...

  10. DAY2新手选品原则及供应商选择

    一.新手选品原则(主要是为了起量) 1.净利润率高(容易起量) 2.发货方便,售后方便(发货,打包方便,不易破损,退货率低) 3.具有细分市场优势(衣服->古代衣服,论文排版) 4.市场规模够大 ...