Implement strStr().

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

1

 class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
return haystack.find(needle)

2 brute force

class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
result = -1
l_needle = len(needle)
l_haystack = len(haystack)
if l_needle == 0 and l_haystack == 0:
return 0
if l_haystack == 0 and l_needle!=0:
return result
if l_needle == 0 :
return 0
for index in range(l_haystack):
if l_haystack - index >= l_needle and needle == haystack[index:index+l_needle]:
result = index
break
return result

3 hash 有待改进

class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
result = -1
l_needle = len(needle)
l_haystack = len(haystack)
if l_needle == 0 and l_haystack == 0:
return 0
if l_haystack == 0 and l_needle!=0:
return result
if l_needle == 0 :
return 0
if l_needle>l_haystack:
return result
hash_needle=hash(needle)
list_haystack=[]
for index in range(l_haystack):
if index<=(l_haystack-l_needle):
list_haystack.append(hash(haystack[index:index+l_needle]))
result=list_haystack.index(hash_needle) if hash_needle in list_haystack else -1
return result

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

  1. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  2. 44. leetcode 28. Implement strStr()

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

  3. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...

  4. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

  5. C# 写 LeetCode easy #28 Implement strStr()

    28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...

  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()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  8. 【LeetCode】28 - Implement strStr()

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

  9. Java [leetcode 28]Implement strStr()

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

随机推荐

  1. 约在CBD,吃饭

    午饭当然是外卖. CBD上班的同仁们不用约,都去了一间叫“大食堂”的餐厅. 它在商业街繁华地段的二楼,有1000平米.你不知道么,餐馆们都躲到找不着的角落,变成了厨房,这里的租金便宜得很.但它不做饭, ...

  2. VB.NET中Form窗体运行时,按ESC退出全屏状态

    1.在其KeyDown事件添加: If e.KeyValue = 27 Then Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable ...

  3. uexQQ插件学习心得

    uexQQ插件学习心得 uexQQ插件的作用:通过qq可以分享图文,音乐,应用到相应的qq空间.支持手机客户端分享和手机webQQ分享.下面我们就来看一看他的一些方法. 我们先说一下分享的步骤,这个步 ...

  4. iOS开发直播需要的准备

    这里我们要研究直播技术首先需要对AVFoundation熟悉掌握 AVFoundation拍照和录制视频 AVFoundation中提供了很多现成的播放器和录音机,但是事实上它还有更加底层的内容可以供 ...

  5. java多线程 生产者消费者模式

    package de.bvb; /** * 生产者消费者模式 * 通过 wait() 和 notify() 通信方法实现 * */ public class Test1 { public static ...

  6. 一个section刷新 一个cell刷新

    一个section刷新   一个cell刷新 //一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2]; [tabl ...

  7. 简单的HttpClient使用

    Httpclient用途很广泛,用来处理各种http请求,这里举个简单的例子 去查询QQ邮件登陆账号检测是的verifycode,一直想怎么能够代码登陆 QQ邮箱,但是QQ的登陆机制做的太TMD牛逼了 ...

  8. 【转】破解Source Insight 3.5.0072过程 附:安装软件+注册机

    转载地址:http://blog.csdn.net/qs_hud/article/details/8884867 注册机及软件下载地址:http://download.csdn.net/detail/ ...

  9. Network网络

    ifconfig 查看服务器网卡名称 ethtool ethXXX 查看网卡具体信息 要测试一个网卡是否真是1000M的,最保险的说用wget测试一个对方的带宽足够大的下载地址 wget http:/ ...

  10. 【PC端】jQuery+PHP实现浏览更多内容(jquery.more.js插件)

    参数说明: 'amount' : '10', //每次显示记录数 'address' : 'comments.php', //请求后台的地址 'format' : 'json', //数据传输格式 ' ...