Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

题解:

 class Solution {
public:
int strStr(string haystack, string needle) {
int pos = -;
int len1 = haystack.size(), len2 = needle.size();
for (int i = ; i <= len1 - len2; ++i) {
int p1 = i, p2 = ;
while (p1 < len1 && p2 < len2) {
if (haystack[p1] != needle[p2])
break;
++p1;
++p2;
}
if (p2 == len2)
return i;
}
return -;
}
};

【LeetCode】028. Implement strStr()的更多相关文章

  1. 【LeetCode】28. Implement strStr() 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...

  2. 【LeetCode】28 - Implement strStr()

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

  3. 【LeetCode】28. Implement strStr() (2 solutions)

    Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...

  4. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

  5. 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...

  6. 【LeetCode】225. Implement Stack using Queues 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...

  8. 【LeetCode】676. Implement Magic Dictionary 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 汉明间距 日期 题目地址:https://le ...

  9. 【LeetCode】225. Implement Stack using Queues

    题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...

随机推荐

  1. Linux下32位与64位数据类型大小

    Redhat Enterprise Linux 32 Redhat Enterprise Linux 64

  2. arcgis for flex map遮罩

    效果1:map的遮罩(对整个map进行遮罩) 效果2:对某个图层进行遮罩

  3. 【转载】openwrt框架分析

    文章出处:http://blog.csdn.net/kingvenll/article/details/27545221 这次讲讲openwrt的结构. 1. 代码上来看有几个重要目录package, ...

  4. Struts2笔记01——基础MVC架构(转)

    原始内容:https://www.tutorialspoint.com/struts_2/basic_mvc_architecture.htm Apache Struts 2是用来创建企业级Java ...

  5. 【Flask】Sqlalchemy 子查询

    ### subquery:子查询可以让多个查询变成一个查询,只要查找一次数据库,性能相对来讲更加高效一点.不用写多个sql语句就可以实现一些复杂的查询.那么在sqlalchemy中,要实现一个子查询, ...

  6. Django 组合索引

    TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join( ...

  7. INSPIRED启示录 读书笔记 - 第22章 原型测试

    物色测试者 1.如果你已经拥有一批特约用户,可以邀请他们参加测试 2.如果是企业级产品,同类产品的展销会是寻找目标用户的好去处 3.可以在分类信息网站上发布广告,征集测试者.征集要求可以写得笼统些,不 ...

  8. [Android]开源中国源码分析之二---DrawerLayout

    从启动界面到主界面之后的效果如图所示,采用的是v4包下的DrawerLayout, activity_main.xml文件如下: <!-- A DrawerLayout is intended ...

  9. CCNA 课程 二

    传输层:两个重要的协议 TCP 和 UDP TCP: 面向连接的协议:在传输用户数据前,先要建立连接 (TCP的三次握手) 错误检查 数据包序列化 可靠性传输:发送的数据需要接受者提供确认,通过报头中 ...

  10. nginx最常见的18道面试题

    Nginx的并发能力在同类型网页服务器中的表现,相对而言是比较好的,因此受到了很多企业的青睐,我国使用Nginx网站的知名用户包括腾讯.淘宝.百度.京东.新浪.网易等等.Nginx是网页服务器运维人员 ...