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. expect结合ssh遍历线上机器

    有个需求,有个文件删除了,但是不确定线上机器还都存不存在 #!/home/work/.jumbo/bin/expect -f set timeout - set mac [lindex $argv ] ...

  2. od破解实例

    百度经验: http://jingyan.baidu.com/article/636f38bb4091e4d6b84610a8.html pc6 http://www.pc6.com/edu/6278 ...

  3. Delphi常用系统函数总结

    Delphi常用系统函数总结 字符串处理函数 Unit System 函数原型 function Concat(s1 [, s2,..., sn]: string): string; 说明 与 S : ...

  4. POJ 1743 Musical Theme

    感觉最近好混乱......各种OJ都刷一点,感觉不太好......尤其是这种英文题 这道题一开始还没有看懂.听了ljh大犇的解释后终于明白了.下面我为英语和我一样的人翻译一下题面: 输入n个数.求最长 ...

  5. 程序开发使用docker部署

    我们公司自己研发了一套 grand-line 系统,使用 docker 来部署项目. 我是第一批小白鼠,一开始网络差,build 一次要半个小时,连接进入 web shell 也很慢,部署一个微信项目 ...

  6. 让你彻底理解 “==”与 Equals

    相信很多朋友在面对,对象判等时经常会犹豫是用“==”还是Equals呢?有时候发现两者得到的结果相同,但有时候有不同, 究竟在什么情况下"==" 会相等,什么情况下Equals会不 ...

  7. javascript 中加’var‘和不加'var'的区别,你真的懂吗?

    没看之前千万别说我是标题党,这个问题真的有好多淫都不懂!!! 大家都看了很多文章,都说避免隐式声明全局变量,就是说声明变量前必须加'var',那加了'var'和不加'var'到底有啥区别呢? 先来看一 ...

  8. web安全——代理(nginx)

    场景 过滤非正常用户使用的http请求. 限制正常用户使用的范围(下载速度.访问频率等). 通过架构规划来提升安全. 能自动解决http请求问题. 解决方案 代理自身的安全 千万不要使用root启动! ...

  9. js基础知识温习:Javascript中如何模拟私有方法

    本文涉及的主题虽然很基础,在很多人眼里属于小伎俩,但在JavaScript基础知识中属于一个综合性的话题.这里会涉及到对象属性的封装.原型.构造函数.闭包以及立即执行表达式等知识. 公有方法 公有方法 ...

  10. 怎样关闭google的自动更新

    谷歌的自动更新很烦人的,只要你点击关于Google Chrome,谷歌就会自动更新成最新版本. 但是sencha框架好像与谷歌29.0以上的兼容性不是很好,所以关闭谷歌自动更新的需求来了,网上很多人说 ...