Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Brute Force算法,时间复杂度 O(mn)

 class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
m = len(haystack)
n = len(needle) if n == 0:
return 0 if m < n:
return -1 for i in range(m-n+1):
if haystack[i:i+n] == needle:
return i return -1

Rabin karp算法时间复杂度可以降低到 O(mn) on average.

haystack: abcdefgh, needle: abc

needle_code = a + b*p + c*p^2 使用sliding window计算haystack_code可以节省计算量。

KMP算法也很快,但是面试一般无法完成。

 def charToInt(c):
return ord(c) - ord('a') + 1 def strStrRK(haystack, needle): m = len(haystack)
n = len(needle) if n == 0:
return 0
if m < n:
return -1 #choose a prime number as base
base = 29 needle_code = 0
for i in range(n):
needle_code += charToInt(needle[i]) * base**i lead = charToInt(haystack[0])
for j in range(m - n + 1): if j == 0:
haystack_code = 0
for i in range(n):
haystack_code += charToInt(haystack[j + i])*base**i
else:
haystack_code -= lead
haystack_code /= base
haystack_code += charToInt(haystack[j + n - 1])*base**(n-1)
lead = charToInt(haystack[j]) if haystack_code == needle_code:
if haystack[j:j + n] == needle:
return j return -1

Leetcode #28. Implement strStr()的更多相关文章

  1. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  2. [LeetCode] 28. Implement strStr() 实现strStr()函数

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  4. [LeetCode] 28. Implement strStr() 解题思路

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. Leetcode 28——Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. [leetcode]28. Implement strStr()实现strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. [LeetCode] 28. Implement strStr() ☆

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

  9. LeetCode——28. Implement strStr()

    题目: class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()){ return ...

随机推荐

  1. Python的高级特性10:无聊的@property

    @property装饰器其实有点无聊,单独拿出来作为一个知识点其实没必要,尽管它可以将方法变成属性,让get和set方法更好用,但是,它破坏了python的简洁(不是代码的简洁而是指语法上). 下面来 ...

  2. asp利用winrar解压缩文件

    '当前文件夹路径 server.MapPath("./") '网站根目录 server.MapPath("/") Dim strZipFolder ' 待压缩的 ...

  3. 使用NIFTI指令画nii图像

    ❤ 关于几种显示工具 mricro:显示出来的左右脑是反着的: mricroN,SPM,xjview,BrainNetViewer:显示出的左右脑是正确的,并且对于做过仿射变换的图像可以自动识别并且校 ...

  4. 硬盘安装win2003

    1.将安装文件放到D盘或E盘,自己指定 2.用U盘或者光盘启动,进入DOS或WINPE吧,转到刚才指定的目录下,运行 winnt32 /syspart:c/marklocalsource/tempdr ...

  5. Linux shell循环

    条件测试 格式 test condition 或 [ condition ] 使用方括号时,要注意在条件两边加上空格,如果有操作符,运算符之间也必须有空格 测试状态:测试的结果可以用$?的值来判断,0 ...

  6. WPF:自动执行"机器人"程序若干注意事项

    企业应用中,经常会遇到一些需要定时自动执行的程序来完成某些功能,比如:自动定时从第三方web service取回数据.定时对历史数据进行清理.定时向ftp上传业务数据... 这类程序,我习惯称为“机器 ...

  7. 做中学之Vim实践教程

    做中学之Vim实践教程 Vim VIM是一个非常好的文本编辑器,很多专业程序员使用VIM编辑代码,即使以后你不编写程序,只要跟文本打交道,都应该学学VIM,可以浏览参考一下普通人的编辑利器--Vim这 ...

  8. promise的学习

    为了解决回调地狱的问题,所以出现了promise的设计思想. promise的三种状态: pending 等待状态 resolved 完成状态 rejected 拒绝状态 promise的三种状态,只 ...

  9. SDRAM读写一字(上)

    SDRAM读写一字 系统设计 SDRAM指令 指令 常量名 CKE CSn RAS CASn WEn 备注 空操作 NOP 1 0 1 1 1   行激活 ACTIVE 1 0 0 1 1   读操作 ...

  10. iOS10-- snapshotViewAfterScreenUpdates 失效

    如果snapshotViewAfterScreenUpdates失效, 用这个方法替代, 不过要自己创建ImageView 替代方式: - (UIImage *)imageFromView:(UIVi ...