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 ...
随机推荐
- 使用iview 的表单组件验证 Upload 组件
使用iview 的表单组件验证 Upload 组件 结果: 点击提交按钮, 没有填的form 项, 提示错误, 当填入数据后提示验证成功 代码: <template> <div id ...
- Spring 资源加载
pom.xml ``` org.springframework spring-core 4.3.14.RELEASE org.springframework spring-beans 4.3.16.R ...
- nginx反向代理实现后端web的读写分离
1.环境 角色 ip 主机名 负载均衡节点 10.0.0.11 nginx-lb01 可读写web01节点 10.0.0.12 nginx-web01 只读web02节点 10.0.0.13 ngin ...
- Linux grep命令 -- 三剑客老三
常用选项 -E :开启扩展(Extend)的正则表达式. -i :忽略大小写(ignore case). -v :反过来(invert),只打印没有匹配的,而匹配的反而不打印. -n :显示行号 -w ...
- Qualcomm_Mobile_OpenCL.pdf 翻译-1
1 前言 1.1 目的 这篇文档的主要目的是,向原始设备制造商(OEMs),独立软件供应商(ISVs),第三方开发者们,提供在基于高通骁龙400系列.600系列,和800系列的手机平台和芯片上进行开发 ...
- Ubuntu下查看so文件的函数列表
Ubuntu下查看so文件的函数列表 可使用如下命令: 1.nm -D XXX.so 2.objdump -tT XXX.so nm libcyusb.so | grep "usb ...
- jmeter解析response里的json对象和数组
1.解析提取json对象 2.解析提取json数组 注意,标红这里是从0开始计数 提取最后一个数组
- SpringBoot 出现内置的Tomcat没有启动的情况
Tomcat未启动,也未报错 pom.xml文件中增加 <dependency> <groupId>org.springframework.cloud</groupId& ...
- 根据IP 自动识别国家和城市
https://www.jianshu.com/p/1b1a018ae729 根据IP 自动识别国家和城市
- MYSQL之数据库初识、安装详解、sql语句基本操作
目录 MYSQL之数据库初识及安装详解 1.什么是数据库? 1.什么是数据?(data) 2.什么是数据库?(databases,简称DB) 2.为什要用数据库? 3.什么是数据库管理系统?(Data ...