LeetCode集锦(十) - 第28题 Implement StrStr

问题

Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = "hello", needle = "ll"
Output: 2Example 2:Input: haystack = "aaaaa", needle = "bba"
Output: -1Clarification:What should we return when needle is an empty string? This is a great question to ask during an interview.For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().复制代码

翻译:

实现strStr ()。
返回haystack中needle的第一次出现的索引,如果针不是haystack的一部分,返回-1。
示例1:
输入:haystack = "hello", needle = "ll"
输出:2
示例2:
输入:haystack = "aaaaa", needle = "bba"
输出:1
澄清:
当needle是空字符串时,我们应该返回什么?这是一个非常适合在面试中问的问题。
对于这个问题,当needle为空字符串时,我们将返回0。这与C的strstr()和Java的indexOf()一致。


解题思路

本题思路很简单,就是让我们实现java的indexof方法,我们根据循环判断haystack中是否有needle字符就行了,当然,可以直接调用java的api。

解题方法

  1. 第一种解题方法,按照思路编辑,代码如下

     if (haystack == null || "".equals(needle)) {return 0;}int len = haystack.length() - needle.length()+1;int needLen = needle.length();for (int i = 0; i < len; i++) {if (haystack.charAt(i) != needle.charAt(0)) {continue;}int m;for (m = 1; m < needle.length(); m++) {if (haystack.charAt(i + m) != needle.charAt(m)) {break;}}if (m == needLen) {return i;}}return -1;
    复制代码

    时间复杂度: 该方案用了循环,循环层数为2,所以O(f(n))=O(Mn),即T(n)=O(n^2)

    空间复杂度: 该方案没有使用额外的空间,所以空间复杂度是O(1);

  2. 第二种解题方法,直接调用api,简单粗暴(当然这个是不符合要求的),代码如下

    if (haystack == null ) {return 0;}return haystack.indexOf(needle);
    复制代码

    时间复杂度: 该方案T(n)=O(1)

    空间复杂度: 该方案没有使用额外的空间,所以空间复杂度是O(1);

总结

本题的大致解法如上所诉,本题只想到了一种方法,第二种方法是不符合要求的,偷懒专用,毕竟都选用了的语言,语言自带的不用白不用

转载于:https://juejin.im/post/5cda3521f265da03b204515b

LeetCode集锦(十) - 第28题 Implement StrStr相关推荐

  1. 【LeetCode从零单排】No28 Implement strStr()

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

  2. LeetCode第28题 实现strStr()之KMP算法(C++)【代码已提交成功】

    目录 初步思路 朴素匹配算法 KMP算法 NEXT数组 利用NEXT数组改进朴素匹配算法 初步思路 这是一道难度为简单的题,所以不熟悉的话可能第一反应就是朴素匹配的算法.但因为考研的时候学过数据结构, ...

  3. strstr函数_leetcode第28题实现strStr()

    点击关注,我们共同每天进步一点点! 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个 ...

  4. leetcode python3 简单题28. Implement strStr()

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十八题 (1)题目 英文: Implement strStr(). Return ...

  5. LeetCode - 28. Implement strStr()

    28. Implement strStr() Problem's Link -------------------------------------------------------------- ...

  6. 【To Do】LeetCode 28. Implement strStr() 和KMP算法

    LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...

  7. leetCode 28. Implement strStr() 字符串

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  8. LeetCode - Easy - 28. Implement strStr()

    Topic Two Pointers, String Description https://leetcode.com/problems/implement-strstr/ Implement str ...

  9. 【LeetCode】28. Implement strStr()

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Implement strStr(). Return the index of the fi ...

最新文章

  1. OpenCV之CvMat Mat IplImage之间相互转换
  2. USB3.0超高速接口应用方案
  3. sendmessage和postmessage的区别
  4. rhel6.5网卡初始化错误解决
  5. java.util.ResourceBundle用法
  6. python语法用到了什么_Python语法的使用和简介
  7. FLASH、SDRAM
  8. Ehcache缓存配置和基本使用
  9. Python 装饰器的八种写法
  10. 一文搞定学术英语写作 (斯坦福SCI论文写作课程笔记)
  11. QC4+充电协议_喜大普奔:高通推出QC3+/4+快充协议,这些处理器均支持
  12. angularJS简介
  13. 用分支限界法解决人员安排问题(Personnel assignment problem)
  14. ARM嵌入式的位绑定原理
  15. ffmpeg java 实时视频流转码
  16. JavaScript编写答题评分功能页面
  17. matlab dx dy dt,dx/dt=y,dy/dt=-sinx,求大神帮忙编一个MATL? 爱问知识人
  18. 英特尔Intel CPU睿频原理探讨
  19. 用了TCP协议,就一定不会丢包吗?
  20. 传统餐桌行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)

热门文章

  1. Google 的 QUIC 华丽转身成为下一代网络协议: HTTP/3.0
  2. Python进程学习笔记-进程创建fork
  3. Week4-作业1:阅读与博客
  4. Sqoop 工具速查表(中文版)--转
  5. 参数调用不会改变参数值
  6. 搜索引擎网址收录地址
  7. php拉查询封装,php实现搜索类封装示例
  8. UVA10047独轮车
  9. Java中对象的储存区
  10. 【数字信号处理】相关函数 ( 周期信号 | 周期信号的自相关函数 )