这是悦乐书的第151次更新,第153篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第10题(顺位题号是28)。给定两个任意字符串haystack、needle,返回haystack中第一次出现needle的索引,如果needle不是haystack的一部分,则返回-1。如果needle为空串,则返回0。例如:

输入:haystack =“hello”,needle =“ll”

输出:2

输入:haystack =“aaaaa”,needle =“bba”

输出:-1

输入:haystack =“”,needle =“”

输出:0

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

haystack依次从0开始截取长度和needle一致的字符串,再与needle比较是否一致,能够匹配上则返回循环到的下标索引,否则返回-1。

public int strStr(String haystack, String needle) {
if ((haystack.isEmpty() && needle.length()>0) || needle.length() > haystack.length()) {
return -1;
}
if (needle.isEmpty() || (haystack.isEmpty()&&needle.isEmpty())) {
return 0;
}
int j = needle.length();
for(int i=0; j<=haystack.length(); i++,j++){
if(needle.equals(haystack.substring(i, j))){
return i;
}
}
return -1;
}

03 第二种解法

直接使用字符串本身的indexOf()方法。

public int strStr2(String haystack, String needle) {
int findResult = haystack.indexOf(needle);
return findResult;
}

04 小结

此题比较简单,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

【算法】LeetCode算法题-Implement strStr的更多相关文章

  1. 乘风破浪:LeetCode真题_028_Implement strStr()

    乘风破浪:LeetCode真题_028_Implement strStr() 一.前言     这次是字符串匹配问题,找到最开始匹配的位置,并返回. 二.Implement strStr() 2.1 ...

  2. leetcode第27题--Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  3. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

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

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

  5. LeetCode OJ:Implement strStr()(实现子字符串查找)

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

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

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

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

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

  8. LeetCode(28)题解:Implement strStr()

    https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...

  9. 【LeetCode】28 - Implement strStr()

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

随机推荐

  1. Perl文件句柄引用

    目前还没介绍Perl的面向对象,所以这节内容除了几个注意点,没什么可讲的. 以前经常使用大写字母的句柄方式(即所谓的裸字文件句柄,bareword filehandle),现在可以考虑转向使用变量文件 ...

  2. 翻译:update语句(已提交到MariaDB官方手册)

    本文为mariadb官方手册:UPDATE的译文. 原文:https://mariadb.com/kb/en/update/ 我提交到MariaDB官方手册的译文:https://mariadb.co ...

  3. 【转载】Sqlserver中查询窗口显示行号

    在Sqlserver中编写语句的时候,有时候因为业务逻辑比较复杂,编写的语句会比较多,此时如果编辑器中显示代码的行号,则对于我们的语句编写有很好的辅助作用.sqlserver默认未开启行号显示功能,可 ...

  4. vue使用element-ui的el-input监听不了回车事件解决

    vue使用element-ui的el-input监听不了回车事件,原因应该是element-ui自身封装了一层input标签之后,把原来的事件隐藏了,所以如下代码运行是无响应的: <el-inp ...

  5. 从零开始学安全(四)●Vmware CentOS 7 添加静态ip联网

    一.虚拟网络编辑器配置 1.VMnet8设置(不需要改动) 2.NAT设置(不需要改动) 3.DHCP设置(CentOS IP地址段设置,不需要改动) 二.虚拟机设置(网络适配器选择NAT模式) 三. ...

  6. SpringBoot 配置 Servlet、Filter、Listener

    SpringBoot 配置 Servlet.Filter.Listener 在SpringBoot应用中,嵌入式的 Servlet 3.0+ 容器不会直接使用 ServletContainerInit ...

  7. TCP/UDP 协议

    传输层建立端口到端口的通信. 网络层的 ip 为我们区分子网,以太网层的 mac 帮我们找到主机.然后大家使用的都是应用程序,你的电脑上可能同时开启qq,暴风影音,等多个应用程序,那么我们通过ip和m ...

  8. 利用Fiddler修改请求信息通过Web API执行Dynamics 365操作(Action)实例

    本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复261或者20170724可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me ...

  9. loadrunner 脚本录制-Action分类

    脚本录制-Action分类 by:授客 QQ:1033553122 Action分类 l . Vuser_init 2. Vuser_end 3.  Action 在lr中用户的初始化操作应该存放在V ...

  10. 「Android」 Surface分析

    本篇针对Surface模块进行分析,从Java层的Activity创建开始,到ViewRoot.WindowsManagerService,再到JNI层和Native层. 首先推荐一个Android源 ...