LeetCode专题-Python实现之第28题: Implement strStr()
相关代码已经上传到github:https://github.com/exploitht/leetcode-python
文中代码为了不动官网提供的初始几行代码内容,有一些不规范的地方,比如函数名大小写问题等等;更合理的代码实现参考我的github repo
1、读题
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
这道题看着有点无语,寻找大字符串里的小字符串,找不到返回-1,这不就是大python里的find()吗?于是我厚着脸皮写出了第一个版本的代码。
2、解题
如下代码,只有一行功能代码不是最屌的地方,最屌的时候提交后发现超过了99.86%的解决方案。忙活半天还不如调用一下自带的字符串处理函数嘛。
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
return haystack.find(needle)
3、符合出题人意图的解法
虽然上面一种方式也找不到啥毛病,不过这样做就不需要动脑了,还是手动实现一下这个find吧。
最先想到的是遍历大字符串,找到和小字符串开头相同的字符,则判断大字符串切片出来的小字符串长度的子串和小字符串是否相等。这样虽然效率不高,逻辑却很简单,先给出代码:
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not needle:
return 0
needle_len = len(needle)
needle_start = needle[0]
for index, value in enumerate(haystack):
if value == needle_start:
if haystack[index:needle_len + index] == needle:
return index
return -1
4、第一次优化
代码解析过几天补充
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
# return haystack.find(needle)
for i in range(len(haystack) + 1):
for j in range(len(needle) + 1):
if j == len(needle):
return i
if i + j == len(haystack):
return -1
if needle[j] != haystack[i + j]:
break
LeetCode专题-Python实现之第28题: Implement strStr()的更多相关文章
- LeetCode专题-Python实现之第27题:Remove Element
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第20题:Valid Parentheses
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第9题:Palindrome Number
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第13题:Roman to Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第7题:Reverse Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第1题:Two Sum
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- MySQL索引背后的数据结构及算法原理(转)
转自:http://blog.codinglabs.org/articles/theory-of-mysql-index.html 摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话 ...
- LOJ 6092
这个题也很没意思 发现q那么大没有用, 不重复的询问有26*n种 所以记录一下就好了 #include<bits/stdc++.h> using namespace std; #defin ...
- 常用jq代码
1. 只允许输入数字,且禁止输入法 <html> <head> <script type='text/javascript' src='../../js/jquery.m ...
- 我理解的websocket
短轮询:客户端发起请求,服务器无论有无消息都返回信息,结束http连接.然后继续发起请求. 长轮询:客户端发起请求,建立连接,直到服务端返回消息response,结束http连接.然后继续发起请求,重 ...
- mybatis 之数据库 include refid ="base_column_list"
mybatis 之数据库 include refid ="base_column_list" 对于刚学习使用SSM框架的新手来说,mybatis中的数据库语句有点不一样,下面便是对 ...
- jquery项目中一些常用方法
1.获取url中的参数 function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + &quo ...
- 真正的ddos防御之道,简单干脆有效!
话说,30G 就各种发博客 BB,唉,坦白说 ,博客园团队真心没见过世面 来 各位 先看图 啥意思呢? 就是哥的 最高防御是 600G. 没错,基本对当时的游戏没啥大的影响,10秒内恢复. 因为时间 ...
- javaweb聊天室源码免费
效果:可擴展源碼,免費技術指導,電話13956301647 链接:https://pan.baidu.com/s/1FWV8DNWacGaEpAQEsWsNaw 提取码:b6gf
- 在IIS上新发布的网站,样式与js资源文件加载不到(资源文件和网页同一个域名下)
在IIS上新发布的网站,网站能打开,但样式与js资源文件加载不到(资源文件和网页是同一个域名下,例如:网页www.xxx.com/index.aspx,图片www.xxx.com/pic.png). ...
- HTML图片标签路径解析
img标签中src属性表示的是引用的图片路径,有两种路径类型: 1. 绝对路径 2. 相对路径. 绝对路径:使用图片在硬盘上的绝对位置来访问图片,通常是从根目录开始,向下一个目录一个目录的寻找. ...