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. 实现对第三方应用任意SO注入

    实现对第三方应用任意SO注入 0x01 应用在Android中运行,从外部对该进程可以进行任意SO文件动态注入,就是应用动态运行我们的SO文件 0x02 基本的逻辑是: 1.    获取目标进程的pi ...

  2. C# Invoke 使用 异步委托

    如果使用多线程,应该会遇到这样的一个问题,在子线程中想调用主线程中(Form1)控件,提示报错! 可以使用Invoke方法调用. this.Invoke(new MethodInvoker(() =& ...

  3. Python学习进程(5)Python语法

        本节介绍Python的基本语法格式:缩进.条件和循环.     (1)缩进: Python最具特色的是用缩进来标明成块的代码. >>> temp=4;x=4; >> ...

  4. MySQL数据文件介绍及存放位置

    怎样查看MySql数据库物理文件存放位置? 使用命令行查找: show global variables like '%datadir%'; 我查找的位置:C:\ProgramData\MySQL\M ...

  5. 最新版express使用时的变化

    原文:http://www.unfish.net/archives/772-20131207.html 很幸运地找到这篇文章,里面的内容讲的非常的细,对于开始着手搭建项目的我来说特别有用.但文中的部分 ...

  6. Ubuntu10.04下的使用使用华为E1750 3G模块

    系   统:Ubuntu 10.04 3G模块:华为E1750 1 安装usb-modeswitch软件 E1750 无线上网卡并没有提供linux环境下的驱动程序,但我们可以通过USB模式转换来让l ...

  7. axios拦截器/http

    Interceptors //处理请求或响应之前拦截请求或响应. //添加一个请求拦截器 axios.interceptors.request.use(function (config) { //在请 ...

  8. Qt编译器

    有两种,MSVC和MINGW Qt 中有两种方式编译,一种是MinGW ,另一种MSVC. MSVC是指微软的VC编译器: MingGW是指是Minimalist GNU on Windows的缩写. ...

  9. 斯坦福机器学习视频笔记 Week9 异常检测和高斯混合模型 Anomaly Detection

    异常检测,广泛用于欺诈检测(例如“此信用卡被盗?”). 给定大量的数据点,我们有时可能想要找出哪些与平均值有显着差异. 例如,在制造中,我们可能想要检测缺陷或异常. 我们展示了如何使用高斯分布来建模数 ...

  10. Windows 2012 系统汉化

    远程登录服务器 控制面板,选择“语言” (Windows 2012 自带的语言包 ) 此处需要等待 安装完成,选择中文 重启后 应该就不需要做其他的操作了