[Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()
https://oj.leetcode.com/problems/implement-strstr/ Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. ===Comments by Dabay===
在长字符串中找子串,当然是KMP算法。既然是三人研究出来的算法,肯定不简单。空了再来研究研究吧。
这里给的是直观的解法,遍历长字符串的时候,开始和短字符串一个一个字符进行比较,如果都比较完了,说明找到了。
如果遍历完了,还没有返回的话,说明没有找到,返回-1。
''' class Solution:
# @param haystack, a string
# @param needle, a string
# @return an integer
def strStr(self, haystack, needle):
i = 0
while i < len(haystack) - len(needle) + 1:
m,n = i,0
while n < len(needle) and haystack[m] == needle[n]:
m += 1
n += 1
if n == len(needle):
return i
i += 1
else:
return -1 def main():
sol = Solution()
haystack = "ahi"
needle = "hi"
print sol.strStr(haystack, needle) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python]28: Implement strStr()的更多相关文章
- 【LeetCode】28. Implement strStr() 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
- 【LeetCode】28 - Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【LeetCode】28. Implement strStr() (2 solutions)
Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- MySql 优化 网上资料
1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得尽 ...
- HTML5中的服务器‘推送’技术 -Server-Sent Events
转帖:http://www.developersky.net/thread-63-1-1.html 一直以来,HTTP协议都是严格遵循Request-Response模型的.客户端发送一个Reques ...
- [置顶] CSDN博客第四期移动开发最佳博主评选
CSDN博客第三期最佳移动开发博主评选圆满结束,恭喜所有上榜用户,为继续展示移动开发方向优秀博主,发掘潜力新星,为移动开发方向的博客用户提供平台,CSDN博客第四期移动开发最佳博主评选开始.同时,获奖 ...
- Coursera-Neural Networks by Geoffrey Hinton
feed-forward networks symmetrically-connection neural networks
- ubuntu 设置网卡为混杂模式 以及网络配置命令
1. ifconfig eth0 promisc 设置eth0为混杂模式. ifconfig eth0 -promisc 取消它的混杂模式 botnet@botnet-virtual-machine: ...
- hdu 4602 Partition(矩阵快速幂乘法)
Problem Description Define f(n) , we have =+++ =++ =++ =++ =+ =+ =+ = totally ways. Actually, we wil ...
- FieldInfo.IsSpecialName Property【转】
Gets a value indicating whether the corresponding SpecialName attribute is set in the FieldAttribute ...
- jQuery特效 隔行变色
1.通过使用onmouseover onmouseout方法 2.变色使用background-color(css)属性 3.变色的标签是td(tr仅仅能使用事件,颜色样式不起作用) 第一种方法 使用 ...
- CSS或者JS实现鼠标悬停显示另一元素
想达到鼠标悬停到元素a上,显示另一个元素b,可以通过css实现也可以通过js实现.js:写两个函数:mouseenter,mouseleave,例如:其中 $("#a").mous ...
- SVN多次重复验证
在MyEclipse中使用svn checkout时,总是弹出密码验证信息,原因是在首次保存密码时与服务器的密码不一致,而每次都是读取的本地密码导致重复验证,这里我们可以删除本地 的密码信息,问题解除 ...