[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 ...
随机推荐
- Scala基础入门-代码碎片
import scala.util.control._ import java.util.Date object Test { def main(args: Array[String]) { // v ...
- Android Studio 没有assets目录的问题
Where to place the assets folder in Android Studio If you are having problems with asset files not ...
- php 表单校验函数库(判断email格式是否正确、http地址是否合法有效、手机号码是否合法)
/** * 表单校验函数库 */ /** * 判断email格式是否正确 * @param $email */ function is_email($email) { return strlen($e ...
- c代码中调用c++,c++代码中调用c代码
注意这里的c调用c++或者c++调用c的意思是.c文件中调用.cpp文件中的代码,或者相反 集成开发环境如vc++6.0或者vs都是通过文件后缀来区别当前要编译的是C代码还是C++代码,然后采用相应的 ...
- ES6新特性-----变量和字符串
一.变量 1. LET 我们都习惯用var 来声明变量,现在还可以用let来声明变量,两者的主要区别是作用域:var声明的变量作用域为包围它的函数,而let声明的变量作用域仅在它所在的块中.(在ES5 ...
- 好的组件,无须太复杂 – KISSY Slide 组件简介
KISSY Slide 组件首页:http://gallery.kissyui.com/slide/1.1/guide/index.html V1.1 New Featurs Slide是一个幻灯切换 ...
- HDU 5700 区间交(线段树)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5700 [题目大意] 给出一个长度为n的数列和m个区间,现在求k个区间,使得他们的区间交内的数列项和 ...
- Android UI ActionBar功能-ActionBarSherlock 的使用
ActionBarSherlock实现了在ActionBar上添加一个下拉菜单的功能,也是App常用的功能之一: ActionBarSherlock是第三方提供的一个开源类库,下载地址:http:// ...
- web基础-web工作原理,http协议,浏览器缓存
1,web工作原理 2,http协议 3,浏览器缓存 4,cookie和session -------------------------------------------------------- ...
- 折叠Collapse插件
实例 折叠(Collapse)插件可以很容易地让页面区域折叠起来.无论您用它来创建折叠导航还是内容面板,它都允许很多内容选项. .collapse 隐藏内容. .collapse.in 显示内容. . ...