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. 用Metasploit破解Mysql用户名和密码

    下面这个方式是普适的,但缺点就是必须要有自己的用户名和密码字典.其原理就是用user.txt与pass.txt的两个文本去不停交叉验证. msf auxiliary(mysql_login) > ...

  2. php中的ip2long和long2ip的理解

    IPv4地址是如何表示的 IPv4使用无符号32位地址,因此最多有2的32次方减1(4294967295)个地址.一般的书写法为用4个小数点分开的十进制数,记为:A.B.C.D,比如:157.23.5 ...

  3. Exploit利用学习1:MS09-001

    目标 IP: Kali系统IP: 1.使用Metasploit框架,在kali终端输入msfconsole打开Metasploit:如下图 2.搜索相关模块:search ms09-001 找到一个可 ...

  4. Hibernate总结1(入门)

    1,官网包简介 整体简介 Project包简介,这个包里有etc文件,这个etc主要包括了配置文件,最主要的是hibernate.cfg.xml文件 lib文件夹包含的是需要加载的依赖jar包,必须加 ...

  5. data:image/png;base64

    大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJ ...

  6. AngularJS中的按需加载ocLazyLoad

    欢迎大家讨论与指导 : ) 初学者,有不足的地方希望各位指出 一.前言 ocLoayLoad是AngularJS的模块按需加载器.一般在小型项目里,首次加载页面就下载好所有的资源没有什么大问题.但是当 ...

  7. C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 拆分表、联系方式的拆分?

    当用户数据有接近10万时,而且多表的关联也比较频繁时,能把大表拆为小表,也会提高系统的性能,I/O.运算性能.当然以后用户数据会更大可能会到30-40万以上,所有有能力时适当拆表,分分合合,合合分分也 ...

  8. C10K 问题引发的技术变革

    C10K 问题引发的技术变革 http://rango.swoole.com/archives/381

  9. md5的C++实现

    一.原理 前一阵子,想知道md5的原理查了一下资料,说得基本都一样,最后让我看懂的是这两个链接: http://blog.csdn.net/qf_study/article/details/26309 ...

  10. Incorrect string value异常解决

    mysql数据库的一个问题 1366-Incorrect string value:'\xE5\x8D\xA1\xE5......' for column 'filename' at row 1 问题 ...