mycode   77.15%

class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
len_1 , len_2 = len(haystack) , len(needle)
for i in range(len_1 - len_2 + 1):
if haystack[i:i+len_2] == needle:
return i
return -1

参考:

要习惯python的.find操作呀

Clarification:

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().

class Solution(object):
def strStr(self, haystack, needle):
if not needle:
return 0
return haystack.find(needle)

leetcode-easy-string-28 Implement strStr()的更多相关文章

  1. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

  2. LeetCode记录之28——Implement strStr()

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

  3. 【leetcode❤python】 28. Implement strStr()

    #-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object):    def strStr(se ...

  4. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  5. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...

  6. 44. leetcode 28. Implement strStr()

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

  7. C# 写 LeetCode easy #28 Implement strStr()

    28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...

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

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

  10. 【LeetCode】28. Implement strStr() 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...

随机推荐

  1. Date对象中的方法

    特殊说明:设置时间的方法,虽然W3C说明传参的范围,在开发过程中,传入的参数不在该范围也是可以的.例如: var t = new Date(), d = t.getDate(); //当天时间往前推2 ...

  2. 1.什么是bat文件

    bat文件是dos下的批处理文件.批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd. 在命令提示下输入批处理文件的名称,或者双击该批处理文件,系统就会调用c ...

  3. linux命令详解——vim

    显示行号:命令模式下set nu 定位到指定行: 命令模式下,:n   比如想到第2行,:2 编辑模式下,ngg  比如想到第5行 5gg(或者5G) 打开文件定位到指定行   vim  +n  te ...

  4. 网上搜到的特别厉害的visio2019激活方法

    原文链接:https://blog.csdn.net/godot06/article/details/94141854 步骤如下: 1.电脑新建一个记事本文件.txt(任何地方都可以) 2.复制下面代 ...

  5. gitlab和jenkins的安装及使用

    gitlab 准备: 最少4G内存 先安装docker软件包然后使用docker search gitlab 查找镜像然后使用docker pull 镜像名:标签名 下载镜像启动容器: docker ...

  6. python文件操作-1.将PDF转成Excel

    # https://www.jianshu.com/p/f33233e4c712 import pdfplumber # 为了操作PDF from openpyxl import Workbook w ...

  7. JAVA项目中的常用的异常处理情况总结

    可能遇见的异常或错误: 检查性异常:最具代表的检查性异常是用户错误或问题引起的异常,这是程序员无法预见的.例如要打开一个不存在文件时,一个异常就发生了,这些异常在编译时不能被简单地忽略. 运行时异常: ...

  8. Python核心技术与实战——二十|Python的垃圾回收机制

    今天要讲的是Python的垃圾回收机制 众所周知,我们现在的计算机都是图灵架构.图灵架构的本质,就是一条无限长的纸带,对应着我们的存储器.随着寄存器.异失性存储器(内存)和永久性存储器(硬盘)的出现, ...

  9. Protobuffer教程

    目录 什么是protobuffer? protobuffer是如何工作的? 为什么不用xml? 1.什么是protobuffer? protobuffer是一种灵活,高效,自动化的机制,用于序列化结构 ...

  10. 程序包管理工具yum

    yum 首先要有一个网络上或本地或远程的yum仓库.然后需要yum安装程序的机器去yum仓库下载yum元数据(包括包信息和依赖信息)到本地的cache里.当需要安装程序的时候,会查看yum源数据里是否 ...