LeetCode28——实现strStr()
6月中下旬辞职在家,7 月份无聊的度过了一个月。8 月份开始和朋友两个人写项目,一个后台和一个 APP ,APP 需要对接蓝牙打印机。APP 和蓝牙打印机都没有搞过,开始打算使用 MUI 开发 APP ,这样学习成本会低一些。但是蓝牙打印机的供应商不提供打印机的指令集,只提供了原生 Android 的 SDK ,因此无奈必须要学习 Android 的原生开发。8 月份加油啊!
LeetCode 题库的第 28 题——实现strStr()
题目如下:

解题代码
该题就是两层循环,第一层循环主要是遍历字符串,第二层循环用来进行匹配。实现代码如下:
int strStr(char* haystack, char* needle) {
int pos = -;
int str1len = strlen(haystack);
int str2len = strlen(needle);
int i, n, j;
if ( str2len == ) {
return ;
}
for ( i = ; i < str1len; i ++ ) {
n = i;
for ( j = ; j < str2len; j ++ ) {
if ( haystack[n++] == needle[j] ) {
if ( j == str2len - ) {
pos = i;
goto EXIT;
}
} else {
break;
}
}
}
EXIT:
return pos;
}
以上是我的解题代码,就是使用两层来进行实现,没有太好的思路,所以解题思路就没什么可说的了。上面的代码,其实还可以减少循环的次数,但是会增加一些代码量,LeetCode 对以上代码执行的时间显示为 1800 多毫秒。那么就增加一个判断,让循环次数少一些,增加的代码如下:
for ( i = ; i < str1len; i ++ ) {
if ( str1len - i < str2len ) {
return pos;
}
n = i;
for ( j = ; j < str2len; j ++ ) {
if ( haystack[n++] == needle[j] ) {
if ( j == str2len - ) {
pos = i;
goto EXIT;
}
} else {
break;
}
}
}
看上面的代码,只是在第一层的 for 循环中增加了一个判断,但是 LeetCode 给出的执行时间是 0 毫秒,是不是很惊人?为什么会这样呢?原因其实很简单,当 needle 很短的时候这个判断是没有太大差别的,但是当 needle 特别长的时候,减少的循环次数就非常明显了。
我的微信公众号:“码农UP2U”

LeetCode28——实现strStr()的更多相关文章
- LeetCode28 Implement strStr()
题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...
- [Swift]LeetCode28. 实现strStr() | Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- leetcode-28.实现strStr()
leetcode-28.实现strStr() 题意 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字 ...
- LeetCode28.实现strStr()
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- LeetCode28.实现strStr() JavaScript
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- leetcode28 strstr kmp bm sunday
字符串匹配有KMP,BM,SUNDAY算法. 可见(https://leetcode-cn.com/problems/implement-strstr/solution/c5chong-jie-fa- ...
- [PHP源码阅读]strpos、strstr和stripos、stristr函数
我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- strstr 函数的实现
strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...
随机推荐
- ActiveMQ是什么,为什么使用MQ
是基于 Java 中的 JMS 消息服务规范实现的一个消息中间件. 1.系统解耦 采用中间件之后,就可以完美解决上述中因为耦合可能导致的问题.系统 A 不用去 关心下层服务调用方的问题. 2. 异步调 ...
- [03]使用 VS2019 创建 ASP.NET Core Web 程序
使用 VS2019 创建 ASP.NET Core Web 程序 本文作者:梁桐铭- 微软最有价值专家(Microsoft MVP) 文章会随着版本进行更新,关注我获取最新版本 本文出自<从零开 ...
- 《DSLR-Quality Photos on Mobile Devices with Deep Convolutional Networks》研读笔记
<DSLR-Quality Photos on Mobile Devices with Deep Convolutional Networks>研读笔记 论文标题:DSLR-Quality ...
- OpenGL光照2:材质和光照贴图
本文是个人学习记录,学习建议看教程 https://learnopengl-cn.github.io/ 非常感谢原作者JoeyDeVries和多为中文翻译者提供的优质教程 的内容为插入注释,可以先跳过 ...
- Web前端——Html常用标签及属性
html 常用的标题等标签就不记录了,只记录一下比较少见的标签以及属性 表格 table td 单元格 tr 表的行 th 表头 td或th可以下面的两个属性达到跨行或跨列 表格跨行 rowspan ...
- C# QRBarCode
1. install-package barcode -v 4.0.2.2; 2. using IronBarCode; class Program { static void Main(string ...
- C# 判断(Excel)文件是否已经打开
using System.IO; using System.Runtime.InteropServices; [DllImport("kernel32.dll")] publi ...
- Dynamics CRM中的操作(action)是否是一个事务(transaction)?
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复168或者20151104可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 以前的博文 微软Dynamics ...
- fatal error: iconv.h: No such file or directory
CodeLite CodeLite编译(使用Cygwin Toolchain)出现如下错误: fatal error: iconv.h: No such file or directory 解决办法 ...
- 开源项目Telegram源码 Telegram for Android Source
背景介绍 Telegram 是一款跨平台的即时通信软件,它的客户端是自由及开放源代码软件.用户可以相互交换加密与自毁消息,发送照片.影片等所有类型文件.官方提供手机版.桌面版和网页版等多种平台客户端. ...