题目链接: https://leetcode-cn.com/problems/implement-strstr/

题目描述

实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1

示例:

示例 1:

输入: haystack = "hello", needle = "ll"
输出: 2

示例 2:

输入: haystack = "aaaaa", needle = "bba"
输出: -1

说明:

needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

思路:

思路1:调用库函数

思路2:

暴力法,时间复杂度:

,
是主字符串,
是模式字符串.

思路3:

如何更好的理解和掌握 KMP 算法?​www.zhihu.com

讲的特别好!


关注我的知乎专栏,了解更多的解题技巧,大家共同进步!

代码:

思路2:

python

class Solution:def strStr(self, haystack: str, needle: str) -> int:if not needle : return 0n1 = len(haystack)n2 = len(needle)if n1 < n2:return -1def helper(i):haystack_p = ineedle_q = 0while needle_q < n2:if haystack[haystack_p] !=  needle[needle_q]:return Falseelse:haystack_p += 1needle_q += 1return Truefor i in range(n1 - n2 + 1):if helper(i):return ireturn -1

python

class Solution:def strStr(self, haystack: str, needle: str) -> int:for i in range(len(haystack) - len(needle)+1):if haystack[i:i+len(needle)] == needle:return ireturn -1

java

class Solution {public int strStr(String S, String T) {int n1 = S.length();int n2 = T.length();if (n1 < n2) return -1;else if ( n2 == 0) return 0;for (int i = 0; i < n1 - n2 + 1; i++ ){if (S.substring(i, i+n2).equals(T)) return i;}return -1;}
}

思路3

python

class Solution:def strStr(self, t, p):""":type haystack: str:type needle: str:rtype: int"""if not p : return 0_next = [0] * len(p)def getNext(p, _next):_next[0] = -1i = 0j = -1while i < len(p) - 1:if j == -1 or p[i] == p[j]:i += 1j += 1_next[i] = jelse:j = _next[j]getNext(p, _next)i = 0j = 0while i < len(t) and j < len(p):if j == -1 or t[i] == p[j]:i += 1j += 1else:j = _next[j]if j == len(p):return i - jreturn -1

java

class Solution {public int strStr(String S, String T) {if (T == null || T.length() == 0) return 0;int[] next = new int[T.length()];getNext(T, next);int i = 0;int j = 0;while (i < S.length() && j < T.length()) {if (j == -1 || S.charAt(i) == T.charAt(j)) {i++;j++;} else j = next[j];}if (j == T.length()) return i - j;return -1;}private void getNext(String t, int[] next) {next[0] = -1;int i = 0;int j = -1;while (i < t.length() - 1) {if (j == -1 || t.charAt(i) == t.charAt(j)) {i++;j++;next[i] = j;} else {j = next[j];}}}
}


同步更新博客:

一起刷LeetCode - 威行天下 - 博客园​www.cnblogs.com

strstr函数_[LeetCode] 28. 实现strStr()相关推荐

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

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

  2. LeetCode 28 Implement strStr()(实现strStr()函数)

    翻译 实现strStr()函数.返回针(needle)在草垛/针垛(haystack)上第一次出现的索引, 如果不存在其中则返回-1.其实也就是说字符串str2在字符串str1中第一次出现的索引而已. ...

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

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

  4. LeetCode 28. 实现strStr()

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  5. leetcode 28.实现strStr()

    题目 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在 ...

  6. LeetCode - 28. Implement strStr()

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

  7. strstr函数_【每日编程176期】实现strStr() II

    每日编程中遇到任何疑问.意见.建议请公众号留言或直接撩Q474356284(备注每日编程) 今日问题: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串, ...

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

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

  9. leetcode 28. 实现 strStr()(kmp)

    实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始).如果不存在,则返 ...

最新文章

  1. 直播APP开发:直播的广告效益和号召力分析
  2. 公布获奖名单推文文案_国学养正·趣味竞答获奖名单公布啦
  3. springboot 配置DRUID数据源
  4. Python之IO编程
  5. 顶级程序员的心得ndash;Coders at Work
  6. 人工智能是未来发展趋势吗 用Python入门怎么样 赶紧看看
  7. 12v服务器电源改可调_赫尔槽试验电源的选择(修订版)
  8. 魅族MX4关闭系统升级Flyme6提示
  9. Java 高级算法——数组中查询重复的数字之二
  10. 【BZOJ4011】【HNOI2015】落忆枫音 题解
  11. 名字作诗(藏头名字作诗)
  12. FACEBOOK改名META,元宇宙救不了FACEBOOK
  13. L5 Limits and Continuity
  14. linux ubuntu 22.04卸载firefox火狐浏览器正确方式
  15. 命令提示符 查看已连接Wifi密码(忘记Wifi密码)
  16. 鸿濛和鸿蒙的区别,鸿蒙_若雪如痕_新浪博客
  17. 详细介绍@GetMapping和@PostMapping的区别
  18. 初三英语关于计算机的作文,2019年中考英语作文范文三篇
  19. laravel 发送邮件随记
  20. 基于tiny4412的Linux内核移植 -- 设备树的展开【转】

热门文章

  1. LiveVideoStackCon 2021上海站 | 参会指南
  2. 《Go语言圣经》学习笔记 第八章 Groroutines和Channels
  3. Navicat Premium 12安装及常用快捷键
  4. 程序员下班后收到工作信息怎么办?
  5. c++中使用 hiredis/hiredis.h
  6. 记一种数据库水平扩展的技巧
  7. go语言的map以及红黑树的map
  8. JAVA程序设计----函数基础2
  9. 【Java】强软弱虚四种引用,弱引用在ThreadLocal中的应用
  10. 牛客网_PAT乙级1008_锤子剪刀布 (20)