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. websocket具体如何使用

    本人是在https://blog.csdn.net/jintingbo/article/details/80755636此地址学习的,所以留做笔记用于之后的学习 现在在写一个工程,是关于监控摄像头的, ...

  2. int 问号的使用

    单问号---为泛型 Nullable<int> 的简写方式. 双问号---用于判断前一个操作数是否为null,如为null则"返回"后一个操作数,否则"返回& ...

  3. python 利用已有Ner模型进行数据清洗合并

    # -*- coding: utf-8 -*- from kashgari.corpus import DataReader import re from tqdm import tqdm def c ...

  4. git push 到 github

    今天来简单整理一下,如何利用git命令把代码提交到GitHub平台上去,当然要提交代码到GitHub上去,您首先得要有GitHub账号,账号如何申请这里就不多做解释了 第一步:先到官网下载git安装包 ...

  5. spring中对JDO的配置

    <!-- persistenceManagerFactory --> <bean id="myPmf" class="org.springframewo ...

  6. javascript 几个易错点记录

    1.清空input标签值得时候,不能用html("")清空,要用val("")清空 2.事件如果绑定多次,触发事件后,回调函数也会执行多次,所有最好把绑定事件的 ...

  7. Java语言基础(3)

    1 算术运算符:+,-,*(乘法),/(除法),%(求余数) 1)只有相同类型的数据才可以进行算术运算,经常使用到自动类型转换和强制类型转换,把参与运算的数据转换为同一个类型,然后再进行算术运算. 案 ...

  8. uestc summer training #3 线段树优化建边

    线段树建边 struct E { int value, modvalue; } a[MAXN << ]; pair<int, int> b[MAXN]; ], r[MAXN & ...

  9. jquery 在将对象作为参数传递的时候要转换成 JSON

    不转换成JSON 会报错  Unexpected identifier 方法: JSON.stringify(对象)

  10. UML(Rational Rose)安装教程【载百度经验】

    ---恢复内容开始--- Win7安装rational rose2007教程 在UML建模的众多工具中,IBM公司的rational rose建模工具可以说是最好用的建模工具了.哪我们又该如何在win ...