这是悦乐书的第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. Docker网络的基本功能操作示例

    一.Docker常用的四种网络模型 1.第一种:使用网络名称空间,但不设置任何网络设备 这种模型中只有lo接口,是一个封闭式的容器,不能与外界进行通信.设置网络模型需要使用 --network 选项来 ...

  2. 第一册:lesson3-4.

    原文: A:My coat and my umbrella please?Here is my ticket. B:Thank you sir.Number five.Here is your umb ...

  3. 好好耕耘 redis和memcached的区别

    观点一: 1.Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等: 2.Redis不仅仅支持简单的k/v类型的数据,同时还 ...

  4. WPF TextBlock IsTextTrimmed 判断文本是否超出

    WPF TextBlock/TextBox 设置TextTrimming情况下 判断 isTextTrimmed(Text 文本是否超出 是否出现了省略号) private bool IsTextTr ...

  5. python之strip()小记

    描述 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列. 注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符. 语法 strip()方法语法: ...

  6. PHP之单例模式

    之前记得有写过PHP的几种这模式.这几天看群里在问单列模式,觉得还是有必要再深入写清楚下..其实单例模式很好理解滴哦 单例模式顾名思义,就是只有一个实例,作为对象的创建模式,单例模式确保某一个类只有一 ...

  7. redis.conf 常见配置介绍

    参数说明redis.conf 配置项说明如下: 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式运 ...

  8. 【Tomcat】Tomcat工作原理

    Tomcat 总体结构 Tomcat 的结构很复杂,但是 Tomcat 也非常的模块化,找到了 Tomcat 最核心的模块,您就抓住了 Tomcat 的“七寸”.下面是 Tomcat 的总体结构图: ...

  9. json字符串和json对象的相互转化

    开发经常要用到json字符串和json对象的相互转化,这里总结常用的两个函数.JSON.parse('字符串'),JSON.stringify('json对象') <script type=&q ...

  10. 关于java中反射的小结

    一.Class 1. Class是一个类,封装了当前对象所对应的类的信息 2.小写class表示是一个类类型,大写Class表示这个类的名称 3.对于每个类而言,JRE 都为其保留一个不变的 Clas ...