leetcode-easy-string-28 Implement strStr()
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()的更多相关文章
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- LeetCode记录之28——Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【leetcode❤python】 28. Implement strStr()
#-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object): def strStr(se ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
- 【LeetCode】28. Implement strStr() 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...
随机推荐
- Linux查看修改文件句柄数
SuSE 11SP3 默认句柄数是1024 1.查看linux的文件句柄数 ulimit -a 2.修改文件句柄数 ①ulimit -n 65535②修改linux系统参数.vi /etc/secur ...
- Go语法的基本使用(三)
// 长度 vs 容量. // 长度是目前里面有几个值 // 容量是最多能放多少个值 func main(){ var a =make(chan int,4) a<-1 a<-2 a< ...
- hadoop安装zookeeper-3.4.12
在安装hbase的时候,需要安装zookeeper,当然也可以用hbase自己管理的zookeeper,在这里我们独立安装zookeeper-3.4.12. 下载地址:https://mirrors. ...
- Windows Electron初探
最近闲来无事,玩玩electron. 1.安装nodejs 下载地址:http://nodejs.cn/download/,下载64位.安装完成后,打开C:\Program Files\nodejs\ ...
- javaweb中的标签的核心标签库的常用标签
//标签的使用使得页面的代码更加简洁,jsp脚本的尽可能少的使用,所以熟练掌握标签对于开发是很有必要的 <%--set设置数据,默认在page域 --%> <c:set var=&q ...
- emwin之BUTTON控件显示位图和流位图出现卡顿延迟的情况
@2019-05-16 [问题] 参照Armfly的emwin教程第46章 BUTTON-按钮控件显示位图和流位图,实际使用时导致界面切换卡顿延迟较大的情况 [环境] F429IGT6 + W9825 ...
- 面试复习题(一)Java系列
(根据自己的理解和根据黑马资料总结—意见不统一我会写上自己的理解) 一.Java面向对象 1.面向对象都有哪些特性以及你对这些特性的理解 继承.封装.多态.(抽象) 2.public,private, ...
- 【CERC 2014 E】2048
题意 2048曾经是一款风靡全球的小游戏. 今天,我们换一种方式来玩这个小游戏. 你有一个双端队列,你只能把元素从左端或从右端放入双端队列中.一旦放入就不得取出.放入后,若队列中有连续两个相同的元素, ...
- 个人学习SpringMVC总结
好记性不如烂笔头! 核心:前端控制器 处理器映射器(HandleMapping) 处理器适配器(HandleAdapter) 视图解析器 流程: 用户从浏览器发起请求,发送到服务器(Tomcat),由 ...
- tarjan算法比较详细的讲解&&tarjan常见疑难解答&&洛谷P2002 消息扩散题解
因为有大佬写的比我更长更具体,所以我也就写写总结一下了 引入: 众所周知,很多图中有个东西名叫环. 对于这个东西很多算法都很头疼.(suchas 迪杰斯特拉) 更深层:环属于强联通分量(strongl ...