这是悦乐书的第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面向对象(3):解构——对象销毁

    本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象销毁 第3篇依赖于第2篇,第2篇依赖于1篇. perl中使用引用计数的方式管理内存, ...

  2. 动手实践Mybatis插件

    前言 Mybatis的插件开发过程的前提是必须要对Mybatis整个SQL执行过程十分熟悉,这样才能正确覆盖源码保证插件运行,总的来说Mybatis的插件式一种侵入式插件,使用时应该十分注意. 在之前 ...

  3. SpringBoot学习(二)-->Spring的Java配置方式

    二.Spring的Java配置方式 Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @Bean Spring的Java配置方式是通过 @ ...

  4. [转]MySQL忘记root密码解决方法

    本文转自:https://www.cnblogs.com/wxdblog/p/6864475.html 今天重新装了一遍MySQL,因为用的是免安装的,所以需要重新设置密码,然后我一通,结果搞得自己也 ...

  5. 在Azure虚拟机上部署FileZilla FTP服务器

    1.开始之前准备的软件 ①一台Azure虚拟机 ②FileZilla服务端安装包 我这边是windows的所以 给个链接 https://filezilla-project.org/download. ...

  6. 操作数据库出现InvalidOpertionException(内部连接致命错误)

    用DataTables时并发访问量较大,单个任务操作(获取数据)时间较长.连接数过多的时候就出现InvalidOpertionException错误.知道哪里有问题那就好办了,对GetDataTabl ...

  7. Winform杂项

    1.控件右键属性:ContextMenuStrip,设置菜单 2.编辑代码:this.treeView1.Nodes.Remove(this.treeView1.SelectedNode);//获取树 ...

  8. mybatis_15整合ehcache

    3.4 整合ehcache Mybatis本身是一个持久层框架,它不是专门的缓存框架,所以它对缓存的实现不够好,不能支持分布式. Ehcache是一个分布式的缓存框架. 什么是分布式 系统为了提高性能 ...

  9. python面向对象学习(一)基本概念

    目录 1. 面向对象基本概念 1.1 过程和函数 1.2 面相过程 和 面相对象 基本概念 2. 类和对象的概念 1.1 类 1.3 对象 3. 类和对象的关系 4. 类的设计 大驼峰命名法 4.1 ...

  10. epoll代码示例

    #include <errno.h> #include <string.h> #include <stdlib.h> #include <sys/types. ...