这是悦乐书的第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. spark之JDBC开发(实战)

    一.概述 Spark Core.Spark-SQL与Spark-Streaming都是相同的,编写好之后打成jar包使用spark-submit命令提交到集群运行应用$SPARK_HOME/bin#. ...

  2. 判断本机ip是电信还是网通

    string location = "0";//0是网通 1是电信 Uri uri = new Uri("http://www.ip138.com/ips138.asp& ...

  3. 基于H5的WebSocket简单实例

    客户端代码: <html> <head> <script> var socket; if ("WebSocket" in window) { v ...

  4. 学习记录---C# Web程序获取客户端电脑信息

    问题描述:由于最近项目需要使用Mac地址与注册码进行加密处理,但是又因为Web程序的局限性不能获取客户端电脑系统信息,当然IE浏览器有一个activex控件他是可以通过Js在前端代码中直接获取的,局限 ...

  5. 46.Linux-分析rc红外遥控平台驱动框架,修改内核的NEC解码函数BUG(1)

    内核版本          :  Linux 3.10.14 rc红外接收类型:  GPIO 类型的NEC红外编码 本章内容 1) rc体系结构分析 2) 分析红外platform_driver平台驱 ...

  6. Eclipse中提示svn: is already locked的解决办法

    eclipse的svn提交不了,报错.提示 svn: is already locked   解决办法:右键项目-------Team------Refresh/Cleanup

  7. python中的tcp

    目录 TCP简介 TCP介绍 TCP特点 TCP与UDP的不同点 udp通信模型 TCP通信模型 tcp客户端 tcp服务器 tcp注意点 TCP简介 TCP介绍 TCP协议,传输控制协议(英语:Tr ...

  8. 如何让gitbook与github仓库关联

    一.账号注册 分别https://legacy.gitbook.com/login和https://github.com/注册账号. https://legacy.gitbook.com/login登 ...

  9. RNP项目遇到的坑

    1.nginx问题 和前端约定了在header中存放登录态k-v,选择的key是带下划线的. nginx 默认会丢弃带下划线的 header. 设置 underscores_in_headers on ...

  10. Deep Learning - 3 改进神经网络的学习方式

    反向传播算法是大多数神经网络的基础,我们应该多花点时间掌握它. 还有一些技术能够帮助我们改进反向传播算法,从而改进神经网络的学习方式,包括: 选取更好的代价函数 正则化方法 初始化权重的方法 如何选择 ...