1、题目

28. Implement strStr()——Easy

Implement strStr().

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

Example 1:

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

Example 2:

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

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

2、我的解答

用python3自带的方法 find 就可以了。。。。

 # -*- coding: utf-8 -*-
# @Time : 2020/2/4 11:03
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 28. Implement strStr().py class Solution:
def strStr(self, haystack: str, needle: str) -> int:
a=haystack.find(needle)
return a
print(Solution().strStr("happynewyear",""))

leetCode练题——28. Implement strStr()的更多相关文章

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

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

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

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

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

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

  4. 44. leetcode 28. Implement strStr()

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

  5. 28. Implement strStr()【easy】

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

  6. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

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

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

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

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

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

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

随机推荐

  1. 执行yum命令报错"Unable to connect to Registration Management Service"

    问题描述 linux上执行yum相关命令时,报无法连接到注册管理服务的错误,具体报错信息如下 [root@aijihe-core-zy-2-3 ~]# yum install gcc Loaded p ...

  2. 实用sql语句合集

    1. 将选取A表的name字段  然后选择A表和B表,最后进行id相等比较 最终得到的是合集 $res = \DB::select("SELECT name FROM users,car_a ...

  3. laravel-middleware中间件常用使用方法

    介绍 中间件提供了一种方便的机制过滤进入应用程序的 HTTP 请求.例如,Laravel 包含一个中间件,验证您的应用程序的用户身份验证.如果用户未被认证,中间件会将用户重定向到登录界面.然而,如果用 ...

  4. JS点击显示隐藏内容

    JS点击显示隐藏密码 思路:获取元素,判断点击,如果DIV显示就隐藏,如果DIV隐藏就显示出来. 1 if(DIV是显示的){ 2 div.style.display='none'; 3 } 4 el ...

  5. JS高级---函数的几个成员

    函数的几个成员 函数中有一个name属性----->函数的名字, name属性是只读的, 不能修改 函数中有一个arguments属性--->实参的个数 函数中有一个length属性--- ...

  6. JS高级---bind方法的使用

    bind方法的使用 //通过对象,调用方法,产生随机数 function ShowRandom() { //1-10的随机数 this.number = parseInt(Math.random() ...

  7. IntelliJ IDEA 2017.3尚硅谷-----生成 javadoc

    Locale:输入语言类型:zh_CN Other command line arguments:-encoding UTF-8 -charset UTF-8

  8. python网络编程(通过tcp或者udp协议通信)

    1.基于tcp协议传送文件: 客户端: import socketimport osimport jsonimport structclient = socket.socket()client.con ...

  9. Java-POJ1010-STAMP

    说良心话,题目不难,但是题目真的很不好懂,解读一下吧 题意: 读入分两行,第一行为邮票面额(面额相同也视为种类不同)以0结束,第二行为顾客要求的面额,以0结束 要求:每个顾客最多拿4张邮票,并求最优解 ...

  10. 516,base64的原理及优缺点

    优点是可以加密,减少了http请求 缺点是需要消耗cpu进行编解码 适用于小图片 base的体积约为原图的4/3