[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 ...
随机推荐
- 前端开发面试题收集 JS
前端开发面试题收集-JS篇 收集经典的前端开发面试题 setTimeout的时间定义为0有什么用? javascript引擎是单线程处理任务的,它把任务放在队列中,不会同步执行,必须在完成一个任务后才 ...
- XML_PULL解析
一.在Android应用中的XML文件来源 1.本地xml文件 本地XML文件可以放在应用根目录assets文件夹.res/xml.res/raw.SDcard卡.应用的data目录等: 除r ...
- java 数字前自动补零实现
/** * 里数字转字符串前面自动补0的实现. * */ public class TestStringFormat { public static void main(String[] args) ...
- centos6.5 Eclipse C/C++开发环境及项目创建测试
1,新建C++ project
- Android UI ActionBar功能-启动ActionBar
官方帮助文档:http://wear.techbrood.com/training/basics/actionbar/index.html ------------------------------ ...
- Web前端性能优化——高频触发事件的防抖
JS 提供了行为层的支持,为用户提供了交互的操作性. 然而,部分事件却常常有意无意的被频繁触发.比方浏览器窗体的 resize 事件.某个元素的 mouseover 事件,假设处理触发事件的回调函数过 ...
- VS2010在C盘下生成的.iTrace文件解决办法 ,c盘偷偷的减少,心很烦啊,找了半天才知道是这个问题
用Visual Studio 2010后发现我的c盘变得越来越小了,刚开始通过优化工具清理c盘,但是无论怎么做都不能将c的内存有效提升,之后一个一个目录的查找之后才知道有个文件夹C:\ProgramD ...
- JavaScript的一些小用法
1.if问题: var a="this test"; if (a == "this test") //这样写的时候执行不下去了,不知为什么. 修改: var a ...
- Android入门——UI(9)
SwipRefreshLayout下拉刷新控件 <?xml version="1.0" encoding="utf-8"?> <android ...
- SQL 语句整理
1. /* SQL CASE 语句写法 * SELECT TABLE1.USER_ID, TABLE1.COMP_CODE, TABLE1.DEPT_CODE, TABLE1.USER_ ...