1.编辑器

我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接

2.第二十八题

(1)题目
英文:
Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

中文:
实现 strStr() 函数。

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-strstr

(2)解法
① 使用现有的方法(.index)
(耗时:40ms,内存:13.9M)

class Solution:def strStr(self, haystack: str, needle: str) -> int:if not needle:return 0if needle in haystack:return haystack.index(needle)return -1

注意:
1.这个是参照了别人的博客写的。
2。.index只能返回第一个出现值的index
3.如果needle是空的’’,则返回0(这一点需要注意)

② 先匹配needle的第一位,如果第一位是匹配的,则看后面的是否是整个needle,如果不是,继续向前搜索,如果是,就return第一位的index。
(耗时:60ms,内存:13.6M)

class Solution:def strStr(self, haystack: str, needle: str) -> int:if not needle:return 0for indx, char in enumerate(haystack):if char==needle[0]:if haystack[indx:indx+len(needle)]==needle:return indxreturn -1

注意:
1.如果for搜索完了整个haystack还是没有return index,则输出-1。

leetcode python3 简单题28. Implement strStr()相关推荐

  1. leetcode python3 简单题225. Implement Stack using Queues

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

  2. leetcode python3 简单题232. Implement Queue using Stacks

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

  3. leetcode python3 简单题69. Sqrt(x)

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

  4. leetcode python3 简单题160. Intersection of Two Linked Lists

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十题 (1)题目 英文: Write a program to find t ...

  5. leetcode python3 简单题136. Single Number

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百三十六题 (1)题目 英文: Given a non-empty array ...

  6. leetcode python3 简单题70. Climbing Stairs

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第七十题 (1)题目 英文: You are climbing a stair ca ...

  7. leetcode python3 简单题53. Maximum Subarray

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十三题 (1)题目 英文: Given an integer array num ...

  8. leetcode python3 简单题58. Length of Last Word

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十八题 (1)题目 英文: Given a string s consists ...

  9. leetcode python3 简单题38. Count and Say

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第三十八题 (1)题目 英文: Given an integer n where 1 ...

最新文章

  1. Oracle 统计信息(1)
  2. linux 系统 UDP 丢包问题分析思路
  3. Android实训日志:基于外部存储的音乐播放器V04
  4. reflective dll injection 反射注入
  5. UI组件之TextView及其子类(五)计时器Chronometer
  6. 中石油训练赛 - Spiral Matrix(找规律)
  7. Windows安全配置加固
  8. kotlin 查找id_Kotlin程序在矩阵中查找偶数和奇数的频率
  9. 7-138 打印沙漏 (20 分)
  10. 95-36-220-ChannelHandler-RejectedExecutionHandlers
  11. python如何写二进制乘法_使用python写乘法口诀表
  12. CC2500规格书参考资料替代
  13. MATLAB 画常见二次曲面汇总
  14. 快速增加闲鱼浏览量,就靠这些方法
  15. 校园综合平台-微信小程序版(整整两个月暑假的成果啊 (•ิ_•ิ))
  16. sicily 1691 Abundance
  17. dd指令打包iso文件 linux_Linux_如何在Linux操作系统下创建ISO镜像文件,1、用dd命令#dd if=/dev/cdrom - phpStudy...
  18. refreshed 问题
  19. IM通讯 即时通讯 交友源码 聊天源码
  20. 你离互联网大公司的距离只有三个月:算法小白的面试成长之旅之路线图

热门文章

  1. 天下手游卡在获取服务器信息,天下手游召唤兽获取与洗练:新手必须知道的事情...
  2. 计算机算法设计与分析 N后问题
  3. 【五级流水线CPU】—— 3. 逻辑、移位与空指令 + 数据冒险RAM解决
  4. [抄]外部奖励对内在动机的侵蚀
  5. 像python一样运行js的__main__
  6. tensorflow中的sequence_loss_by_example
  7. LeetCode 76. 最小覆盖子串 (滑动窗口哈希表)
  8. Team Foundation Server
  9. AOJ GRL_1_A: Single Source Shortest Path (Dijktra算法求单源最短路径,邻接表)
  10. java基础篇--------------配置jdk的环境变量