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 文中代码为了不动官网提供的初始 ...
随机推荐
- Android EventBus技能点梳理
EventBus为Github上的开源项目,地址:https://github.com/greenrobot/EventBus 疑问:1. 现在都是Android Studio创建的项目,如何导入这些 ...
- Python3 文件
f=open('C:\\Users\\fengx\\Desktop\\sharing\\test.txt') 如果打开文件的格式不匹配,可能会报如下错: >>> open('C:\U ...
- IndentityServer4
官网: https://identityserver4.readthedocs.io/en/latest/index.html 比较好的中文博客: 晓晨Master: https://www.cnbl ...
- tmux使用中出现的问题和解决方式
常用操作: tmux ls 看当前都有哪些sessiontmux new -s my1 创建窗口,名为my1ctrl+B,D 退出窗口 (这个就是同时按ctrl和B,然后松开后再按D键)tmux at ...
- angular.isUndefined()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [LeetCode] Race Car 赛车
Your car starts at position 0 and speed +1 on an infinite number line. (Your car can go into negati ...
- 19南昌网络赛L
校赛打杂没施展开. 题意:一开始给你一颗 (0,0)到(0,l)的树. 这棵树每一年会长出来三个幼芽(雾),长度均为l/4,方向分别是左转60,右转60,和不变. 年份<=14 考虑3^14直接 ...
- docker kubernetes Swarm容器编排k8s CICD部署
1docker版本 docker 17.09 https://docs.docker.com/ appledeAir:~ apple$ docker version Client: Docker En ...
- SELECT 语句
常见表的操作 查看数据库的表 show table 查看表结构 desc 表名 删除表 drop table表 修改表的结构 添加列 alter table 表名 add 列名 ...
- unittest测试套件
测试套件就是测试集,测试集是测试用例的集合. a.按用例顺序执行(addtest) 当addtest与unittest的测试规则冲突时,仍然按照ASCII码的顺序执行. import unittest ...