这是悦乐书的第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. GVRP 的工作机制和工作模式

    GVRP 简介 GVRP 基于 GARP 的工作机制来维护设备中的 VLAN 动态注册信息,并将该信息向其他设备传播:当设备启动了 GVRP 之后,就能够接收来自其他设备的 VLAN 注册信息,并动态 ...

  2. [转]c# System.IO.Ports SerialPort Class

    本文转自:https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport?redirectedfrom=MSDN& ...

  3. .net 模拟登陆 post https 请求跳转页面

    AllowAutoRedirect property is true, the Referer property is set automatically when the request is re ...

  4. SQL SERVER PIVOT与用法解释

    通俗简单的说:PIVOT就是行转列,UNPIVOT就是列传行 在数据库操作中,有些时候我们遇到需要实现“行转列”的需求,例如一下的表为某店铺的一周收入情况表: WEEK_INCOME(WEEK ),I ...

  5. BurpSuite 各模块使用

    Proxy  代理 对浏览器进行代理 对浏览器增加代理服务器 可以对http 请求进行监视 intercept is on 进行监控  off 不监控 可以任意修改 对任意的网络请求 进行爬虫 在 s ...

  6. python - unittest 单元测试学习

    单元测试 单元测试是用来对一个模块.一个函数或者一个类进行正确性检验的测试工作 比如对Python中的abs 的测试 输入正数: 比如 1, 2, 3, 返回值不变 输入负数: 比如 -1, -2, ...

  7. 33.QT-UTF8,GBK互转

    首先需要用到QString的静态成员函数来获取字符数组: QByteArray QString::toLocal8Bit () ; //获取字节数组对象 char * QByteArray::data ...

  8. 【Java每日一题】20170313

    20170310问题解析请点击今日问题下方的“[Java每日一题]20170313”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; import jav ...

  9. Java中单例实现

    1:.经典懒汉: 代码如下: package org.pine.test; public class Person { private String name; private int age; pu ...

  10. es6 语法 (函数扩展)

    //函数参数默认值(more值后不能有参数) { function test(x,y = 'world'){ console.log('默认值',x,y); } test('hello');// he ...