题目:

第一次提交:

class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if not len(needle):
return 0
for i in range(len(haystack)):
if i+len(needle)<=len(haystack):
if haystack[i:(i+len(needle))]==needle:
return i
return -1

方法二: Sunday 平均O(N),最坏O(MN)

class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if not needle:return 0
from collections import defaultdict
dic = defaultdict(int)
for i,v in enumerate(needle):
dic[v] = len(needle) - i
idx = 0
while idx + len(needle) <= len(haystack):
cur = haystack[idx:idx+len(needle)]
if cur == needle:
return idx
else:
if idx+len(needle) >= len(haystack):
return -1
cur_c = haystack[idx+len(needle)]
if dic.get(cur_c):
idx += dic[cur_c]
else:
idx += len(needle)+1
return -1

方法三:kmp O(m + n)

class Solution:
def strStr(self, t, p):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not p : return 0
_next = [0] * len(p) def getNext(p, _next):
_next[0] = -1
i = 0
j = -1
while i < len(p) - 1:
if j == -1 or p[i] == p[j]:
i += 1
j += 1
_next[i] = j
else:
j = _next[j]
getNext(p, _next)
i = 0
j = 0
while i < len(t) and j < len(p):
if j == -1 or t[i] == p[j]:
i += 1
j += 1
else:
j = _next[j]
if j == len(p):
return i - j
return -1

next数组的优化:

class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if not needle:return 0
def getNext(s):
next_ = [0] * len(s)
next_[0] = -1
i = 0
j = -1
while i < len(s) - 1:
if j == -1 or s[i] == s[j]:
j += 1
i += 1
if s[i] == s[j]:
next_[i] = next_[j]
else:
next_[i] = j
else:
j = next_[j]
return next_
next_ = getNext(needle)
i,j = 0,0
while i < len(haystack) and j < len(needle):
if j == -1 or haystack[i] == needle[j]:
i += 1
j += 1
else:
j = next_[j]
if j == len(needle):
return i - j
return -1

注意:

说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

方法二:KMP算法,KMP的整体时间复杂度为O(m + n)。

算法详解:https://blog.csdn.net/v_july_v/article/details/7041827

leetcood学习笔记-28-KMP*的更多相关文章

  1. [原创]java WEB学习笔记28: 会话与状态管理Cookie 机制

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  2. struts2视频学习笔记 28(OGNL表达式)

    课时28 OGNL表达式 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts 2框架使用OGNL作为默认的表达式语 ...

  3. Kali学习笔记28:Burpsuite(下)

    文章的格式也许不是很好看,也没有什么合理的顺序 完全是想到什么写一些什么,但各个方面都涵盖到了 能耐下心看的朋友欢迎一起学习,大牛和杠精们请绕道 扫描: 上一篇介绍到了爬网,那么到这里我以及爬取了一个 ...

  4. C语言实例解析精粹学习笔记——28

    实例28:从键盘读入实数 题目要求: 编制一个从键盘读入实数的函数readreal(double *rp).函数将读入的实数字符列转换成实数后,利用指针参数rp,将实数存于指针所指向的变量*rp. 思 ...

  5. leetcood学习笔记-20

    python字符串与列表的相互转换   学习内容: 1.字符串转列表 2.列表转字符串 1. 字符串转列表 str1 = "hi hello world" print(str1.s ...

  6. leetcood学习笔记-14*-最长公共前缀

    笔记: python if not   判断是否为None的情况 if not x if x is None if not x is None if x is not None`是最好的写法,清晰,不 ...

  7. leetcood学习笔记-54-螺旋矩阵

    题目描述: 第一次提交: class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: j,x = 0 ...

  8. C++学习笔记28:运行期型式信息

    RTTI 运行期标识对象的型式信息 优势:允许使用指向基类的指针或引用自如地操作派生类的对象 typeid:获取表达式的型式:type_info:型式信息类 头文件:typeinfo 对象转型模板 d ...

  9. CUBRID学习笔记 28 执行sql脚本文件

    一下命令在csql下执行. insert_commands.sql为sql脚本文件 ;CL ;READ insert_commands.sql ;RU 第一行的cl 清空命令缓存,等同clear第二行 ...

随机推荐

  1. docker-ce创建gitlab-ce容器笔记

    前言 vagrant + ubuntu 16.04 设置 apt 源 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak sudo vim ...

  2. jQuery查阅api手册

    原文&出处:jQuery API 3.3.1 速查表  --作者:Shifone http://jquery.cuishifeng.cn/

  3. java--substring内存溢出问题

    public class SubStringDemo { //substring() /** * jdk6 当调用 substring() 方法时,创建了一个新的String对象,但是string的v ...

  4. leetcode-165周赛-1275-找出井字棋的获胜者

    题目描述: 自己的提交: class Solution: def tictactoe(self, moves: List[List[int]]) -> str: p = [[0] * 3 for ...

  5. Struts2 简单的增删改查

    1:主页面 <a href="emp-list">emp-list</a> <br> 然后到struts.xml文件中找到对应的emp-list ...

  6. 服务器一般达到多少QPS比较好?

    每秒查询率QPS是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准,在因特网上,作为域名系统服务器的机器的性能经常用每秒查询率来衡量. 原理:每天80%的访问集中在20%的时间里,这20%时 ...

  7. 避开PCB假八层结构的温柔陷阱---浅谈六层板的叠层

    https://blog.csdn.net/qijitao/article/details/51505611 作者:王辉东   一博科技高速先生团队队员 在<PCB的筋骨皮>一文中,我们提 ...

  8. DPL, CPL及RPL之间的关系

    DPL: Descriptor Privilege Level 1) GDT/LDT表中的描述符 GDT/LDT表中的描述符,描述的是一段内存. 其中的DPL代表着GDT/LDT中的描述符描述的内存段 ...

  9. jmeter 响应超时时间设置 压力增大,不能正常退出全部线程

    当压力增大会出现connect timeout error 压力增大,不能正常退出全部线程: 解决办法:http request default--advance--timeouts 如填写1,表示大 ...

  10. Dubbo 系列(07-1)集群容错 - 服务字典

    Dubbo 系列(07-1)集群容错 - 服务字典 [toc] Spring Cloud Alibaba 系列目录 - Dubbo 篇 1. 背景介绍 本篇文章,将开始分析 Dubbo 集群容错方面的 ...