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. Express multer 文件上传

    npm multer 文件上传 Express app 范本就不写了,仅记录一下上传部分的代码. const fs = require('fs'); const express = require(' ...

  2. 误删除/dec/zero,/dev/null

    误删除/dev/zero [root@MYSQL-MONGO145 dev]# mknod /dev/zero c 1 5[root@MYSQL-MONGO145 dev]# chmod 666 /d ...

  3. js截取字符串相关的知识点

    截取字符串中的数字 1.使用parseInt() var str ="4500元"; var num = parseInt(str); console.log(num);//450 ...

  4. 解决postgresql数据库localhost可以连接,ip连接不了的问题

    解决:windows环境下,postgresql数据库,localhost可以连接,ip地址连接不了. 解决办法: 1.打开postgresql安装目录下的配置文件 pg_hba.conf       ...

  5. postgres中的函数

    1.编写一个只有入参,没有出参的函数: CREATE OR REPLACE FUNCTION add(a NUMERIC, b NUMERIC) RETURNS NUMERIC AS $$ SELEC ...

  6. Markdown,让你的代码高亮起来

    当你的审美提高或者习惯了Linux下的黑色代码高亮,已经不满足与博客园原有的代码高亮,那么这篇博客就是你的选择. 代码块高亮 代码块Highlight /* 使用了Monokai Sublime的黑色 ...

  7. ​TDD明白了,ATDD测试到底是什么?

    随着敏捷开发的蓬勃发展.遍地开花,TDD(Test Drive Development测试驱动开发)的概念已经深入软件研发从业者的心中. TDD讲究的是:“测试在先.编码在后”.有别于以往的“先编码. ...

  8. cmd完成拷贝文件,并生成两个快捷脚本

    @echo off@echo ------------------------------ @echo 正在创建目录 color 03if exist y:\00程序数据备份 ( md y:\00程序 ...

  9. Mac破解软件 “XXX”意外退出 奔溃解决方法

     最近很多破解软件提示“XXX”意外退出.这是因为苹果在7月12日删除了TNT的证书,所以大部分TNT破解的Mac软件会出现无法打开,提示意外退出. 目前的解决办法是在终端执行命令: 1.首先安装“A ...

  10. MyEclipse使用教程:导航代码(一)

    [MyEclipse CI 2019.4.0安装包下载] 无论是在文件之间导航还是在文件中导航,都可以使用大量导航工具来加快工作流程.目前这些导航工具可在MyEclipse,CodeMix中使用. 快 ...