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. Yii2.0数据库查询实例(三)

    常用查询: // WHERE admin_id >= 10 LIMIT 0,10 User::find()->])->offset()->limit()->all() / ...

  2. http://www.uupoop.com/ps/

    网页版PS,在线PS 基本的PS功能都有,最重要的一点是快,网页版的嘛,哼哼!

  3. Linux CentOS7安装Mysql5.7

    一.下载mysql mkdir /home/install #创建install目录 在/home/install目录下下载mysql5.7 wget https://cdn.mysql.com//D ...

  4. VM上安装苹果虚拟机

    用了太久的Windows系统,看着Mac OS X的惊艳,相信很多朋友也和我一样,总想着能把玩一把Mac OS X系统吧?如果只是为了体验一下Mac OS X系统而购买一套Mac电脑,那是土豪做的事. ...

  5. qt5.4.1的imx6编译

    2.到https://download.qt.io/archive/qt/5.4/5.4.1/single/下载源码包qt-everywhere-opensource-src-5.4.1.tar.gz ...

  6. Docker容器技术-第一个容器

    一.第一个容器 1.Docker版本 A.community-edition社区版 Docker CE是免费的Docker产品的新名称,Docker CE包含了完整的Docker平台,非常适合开发人员 ...

  7. 转移灶,原发灶,cfDNA的外显子测序得到的突变点的关系

    文章名称:Exome Sequencing of Cell-Free DNA from Metastatic Cancer Patients IdentifiesClinically Actionab ...

  8. windows简单使用etcd

    一.下载安装选择版本 https://github.com/coreos/etcd/releases 二.解压 三.首先开启etcd 1.进入在etcd解压的目录中 2.etcd.exe 没有erro ...

  9. 机器学习相关知识整理系列之三:Boosting算法原理,GBDT&XGBoost

    1. Boosting算法基本思路 提升方法思路:对于一个复杂的问题,将多个专家的判断进行适当的综合所得出的判断,要比任何一个专家单独判断好.每一步产生一个弱预测模型(如决策树),并加权累加到总模型中 ...

  10. INSPIRED启示录 读书笔记 - 第6章 招聘产品经理

    产品经理应有的特质 个人素质和态度:技术可以学习,素质却难以培养,有些素质是成功的产品经理必不可少的 对产品的热情:对产品有一种本能的热爱,是夜以继日克服困难.完善产品的动力 用户立场:能换位思考,能 ...