返回 haystack 中第一次出现针的索引,如果针不是 haystack 的一部分,则返回 -1。

寻找大字符串里的小字符串,找不到返回-1,这不就是大python里的find()吗?于是我厚着脸皮写出了第一个版本的代码。

当针为空字符串时,我们应该返回什么?这是面试时要问的一个很好的问题。 为了解决这个问题,当needle为空字符串时,我们将返回0。这与 C 的 strstr() 和 Java 的 indexOf() 一致。

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

Example 3:

Input: haystack = "", needle = ""
Output: 0
class Solution(object):def strStr(self, haystack, needle):""":type haystack: str:type needle: str:rtype: int"""return haystack.find(needle)
class Solution(object):def strStr(self, haystack, needle):return haystack.find(needle)

28 Implement strStr() @Python相关推荐

  1. LeetCode - 28. Implement strStr()

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

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

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

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

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

  4. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

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

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

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

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

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

  7. 【LeetCode】28. Implement strStr()

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

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

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

  9. [leetcode] 28. Implement strStr() 解题报告

    题目链接:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the fi ...

  10. leetcode 28. Implement strStr() 实现strStr()

    C++代码,题目相对不是很难 1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if(need ...

最新文章

  1. Visual Studio警告IDE0006的解决办法
  2. 赠书:啥是指标陷阱?很多就出现在你的身边!
  3. Spring 基于Java的Bean声明
  4. 2021年程序员可以做哪些副业?
  5. python的重点_python知识点
  6. extJS 中 ext.data 介绍
  7. MySQL之数据库基本操作语句
  8. PDE2 three fundamental examples
  9. py 的 第 34 天
  10. 结对项目 浪曦计时器
  11. 苹果mac专业的图像后期处理软件:Lightroom Classic
  12. 记2021沙尘漫天的春
  13. 输入FILCO蓝牙键盘的PIN
  14. PHP开发环境的搭建
  15. php中escape和unescape
  16. to_Date()用法
  17. java基础巩固-宇宙第一AiYWM:为了维持生计,架构知识+分布式微服务+高并发高可用高性能知识序幕就此拉开(一:总览篇)~整起
  18. Sentinel核心源码解析
  19. python基础(六)函数
  20. 【计算机毕业设计】在线考试系统

热门文章

  1. NLog 在winform和asp.net下使用快速攻略 .
  2. vue 配置sass、scss全局变量
  3. Kafka 中 partition replica 默认分配机制
  4. SQL优化常用方法51
  5. 科技部再公布网络安全等10项国家重点研发计划
  6. 演示使用Metasploit入侵Windows
  7. HTTP协议是什么?(及get和post请求的区别)
  8. hadoop交流群261039241
  9. Windows 7 SP1开发完毕 已进入测试阶段
  10. stm32 整数加法循环时间_剑指 Offer 65. 不用加减乘除做加法 leetcode 剑指offer系列...