[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性能优化学习笔记(3)常见sql语句优化
一.max()优化mysql> explain select max(payment_date) from payment;+----+-------------+---------+----- ...
- php 截取字符串
/** * 方法库-截取字符串-[该函数作者未知] * @param string $string 字符串 * @param int $length 字符长度 * @param string $dot ...
- 限制TextBox输入,只能输入整数
public class TextBoxInt : TextBox { public TextBoxInt() { KeyDown += TextBoxInt_KeyDown; TextChanged ...
- 使用plist的好处
首先:帮助节省内存.OpenGL ES纹理要求宽和高都是2的n次幂的倍数.我们可以考虑将小的图片拼大图片,然后统一加载. 其次:提高渲染速度.OpenGL ES要求切换的纹理越少越好,将图片拼成大图 ...
- 客户端持久化解决方案:indexedDB
客户端持久化解决方案:indexedDB indexedDB适合大量的结构化的数据存储:打开数据库和获取数据对象都是异步的: 需要开启事务,访问的objectStore都要是在开启的事务中. 数据库结 ...
- SpringDataRedis事务处理
public Long leftPush(V value) { return this.ops.leftPush(this.getKey(), value); } public Long leftPu ...
- 中国市场 Android App 兼容性报告
由于手机操作系统的不同,以及操作系统版本之间的差异,使得真机测试这个过程尤其复杂,涉及终端.人员.工具.时间.管理等方面的问题,Android系统的设备因操作系统多样性和终端类型的庞杂,问题尤为复 ...
- Unity3D 物体移动到点击位置
using UnityEngine;using System.Collections; public class MoveToClick : MonoBehaviour{ public GameObj ...
- fuel Explain
http://docs.mirantis.com/openstack/fuel/fuel-5.1/ https://software.mirantis.com/quick-start/ https:/ ...
- Linux学习笔记2:如何快速的学习使用一个命令
Linux 分层 内核 库: .so 共享对象,windows:dll 动态链接库 应用程序 Linux的基本原则: 1.由目的单一的小程序组成:组合小程序完成复杂任务: 2.一切皆文件: 3.尽量避 ...