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 文中代码为了不动官网提供的初始 ...
随机推荐
- php |= 什么意思
- Machine Learning学习资源
引申:非原创,转载来自:https://blog.csdn.net/ptkin/article/details/50995140
- github-新建文件夹
1,进入仓库“ sstruggle.github.io ”中,在该仓库页面中找到“ Create new file ”,如图: 2,在创建新文件页面,输入“ js/ ”,github默认为是一个文件夹 ...
- BZOJ5304 : [Haoi2018]字串覆盖
离线处理所有询问. 对于$r-l\leq 50$的情况: 按照串长从$1$到$51$分别把所有子串按照第一位字符为第一关键字,上一次排序结果为第二关键字进行$O(n)$基数排序. 同理也可以用上一次比 ...
- go语言数据库操作, gorm框架
type User struct{ ID uint `gorm:"primary_key"` Name string Age int Birthday time.Time AddT ...
- CSS grayscale滤镜+SVG使图片变黑白实例页面
http:/CSS 地址:/www.runoob.com/cssref/css3-pr-filter.html CSS代码: .gray { -webkit-filter: grayscale(%); ...
- 快速幂 ,快速幂优化,矩形快速幂(java)
快速幂形式 public static int f(int a,int b,int c){ int ans =1; int base=a; while(b!=0){ if((b&1)!=0) ...
- 201771010126 王燕《面向对象设计 java》第十五周实验总结
第一部分 理论部分 ◼ JAR文件◼ 应用程序首选项存储◼ Java Web Start JAR文件: 1.Java程序的打包:程序编译完成后,程序员将.class文件压缩打包为.jar文件后,GU ...
- maven 禁止连接外网仓库
有些内网机器不能连外网的情况下,因为依赖的项目pom配置问题,mvn package时仍会尝试请求外网的repo(比如默认中央repo或oss). 此时配置 settings.xml 为自己内网rep ...
- cadence元件放置方法
在导入网表之后,需要放置元件,介绍几种常见的放置元件的方法和常用的几种元件操作方法.