【LeetCode】028. Implement strStr()
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()的更多相关文章
- 【LeetCode】28. Implement strStr() 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...
- 【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() (2 solutions)
Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
- 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...
- 【LeetCode】225. Implement Stack using Queues 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- 【LeetCode】676. Implement Magic Dictionary 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 汉明间距 日期 题目地址:https://le ...
- 【LeetCode】225. Implement Stack using Queues
题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...
随机推荐
- Apache NiFi 开发 安装说明
系统环境: vmware安装的centos6.7虚拟机 jdk1.8版本 maven库3.3.9版本(在使用源码编译启动的时候需要修改配置文件与当前使用的maven版本匹配,最低使用版本好像是3.1. ...
- Linux Shell基础 通配符
通配符 在 Bash 中,如果需要模糊匹配文件名或目录名,就要用到通配符.下面为常用的通配符. 表 1 通配符 通配符 作 用 ? 匹配一个任意字符 * 匹配 0 个或任意多个任意字符,也就是可以匹配 ...
- python + Streaming框架的MR实践与优化
Streaming是Hadoop提供的一个可以使用其他编程语言来进行MR编程的API,它使用Unix标准输入输出作为Hadoop和其他编程语言的开发接口,非常轻便.而开发者可以选择自己擅长的编程语言, ...
- Java智能图表类库JChartLib使用介绍
http://www.codeceo.com/article/java-jchartlib.html JChartLib是一款基于Java的智能图表类库,JChartLib不仅有着漂亮的外观,而且支持 ...
- 给嵌入式linux串口添加密码的一些总结
要添加串口登录密码就需要修改 /etc/inittab 文件的配置 但是不同的开发板情况不一样,有的是没有这个文件的,BusyBox会检查/etc/inittab文件是否存在, 如果此文件不存在, B ...
- FullPage.js全屏滚动插件
一.介绍 fullPage.js是一个基于jQuery的插件,他能够很方便.很轻松的制作出全屏网站,主要功能有: 1.支持鼠标滚动 2.多个回调函数 3.支持手机.平板触摸事件 4.支持CSS3动画 ...
- 医院内外网之间通过网闸交互,通过端口转发加nginx代理实现内网访问外网
首先介绍下主要需求,很简单,就是要在医院his系统内嵌公司的平台,实现内网直接访问外网 这是院方给我提供的网闸相关配置,105是医院内网的服务器,120是外网的服务器,中间通过网闸配置的几个端口实现互 ...
- 关于Json如何转换成对象及获值问题!
var result = eval('('+result+')'); result为Json 转换成var result对象,可以 if(result.success){ window.locatio ...
- UVA 11731 Ex-circles (外切圆)
题意:给你三角形的三条边,求图中DEF的面积和阴影部分的面积. 题解:一些模板,三角形的旁切圆半径:.与 三旁心为 #include<set> #include<map> #i ...
- Python之面向对象总结
一.面向对象 1.面向过程 a.优点:极大的降低了写程序的复杂度,只需要顺着执行的步骤,堆叠代码即可 b.缺点:一套流水线或者流程就是来解决一个问题,代码就是牵一发而东莞全身 2.面向对象 a.优点: ...