[LeetCode]题解(python):028-Implement strStr()
题目来源:
https://leetcode.com/problems/implement-strstr/
题意分析:
输入两个字符串haystack和needle,如果needle是haystack的一个子串,那么返回这个子串在haystack出现的第一个位置,否则返回-1.
题目思路:
这个题目是简单题,直接暴力解决就可以了。从i=0出发,如果遇到haystack[i] == needle[0],那么判断从这个出发能不能构成needle,如果可以则返回i。直到i到最后一个字符的长度小于needle的长度。如果前面没有返回值,那么返回-1.时间复杂度是(O((m - n) * n)).
代码(python):
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
size1 = len(haystack)
size2 = len(needle)
if size2 == 0:
return 0
if size1 < size2:
return -1
i = 0
while i < size1:
if size1 - i < size2:
return -1
if haystack[i] == needle[0]:
j = 1
while j < size2:
if haystack[i + j] != needle[j]:
break
j += 1
if j == size2:
return i
i += 1
return -1
转载请注明出处:http://www.cnblogs.com/chruny/p/4893126.html
[LeetCode]题解(python):028-Implement strStr()的更多相关文章
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 【LeetCode】028. Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode 028 Implement strStr()
题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- leetcode第27题--Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- 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()
题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if nee ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- 028 Implement strStr() 实现 strStr()
实现 strStr().返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 .例 1:输入: haystack = ...
随机推荐
- Bootstrap禁用响应式布局
在Bootstrap中极其重要的一个技术内容便是响应式布局了,一次编码针对不同设备终端的强大能力使得响应式技术愈发流行. 不过正所谓"萝卜青菜各有所爱",如果你想要使用Bootst ...
- 破解win7开机密码,又不取消密码
Win7开机密码破解方法 1. 进入pe.(可用系统盘,或者u盘启动) 2.进入c:\windows\system32下 更改magnify.exe和cmd.exe的所有者为administrator ...
- Android系统的“程序异常退出”[转]
在应用运行过程中,有很多异常可能会发生,而我们希望在异常发生的时候第一时间的保存现场. 如何处理未捕获的异常呢? 首先我们要实现一个接口 java.lang.Thread.UncaughtExcep ...
- ios蓝牙开发(四)BabyBluetooth蓝牙库
BabyBluetooth 是一个最简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx. 特色: 基于原生CoreBluetooth框架封装的轻量级的开源库,可以帮你 ...
- BootStrap 智能表单系列 三 分块表单配置的介绍
相信广大博友肯定碰到过一个编辑页面分了很多块的情况,智能表单插件已经为您支持了这种情况, 代码如下(链接地址:https://github.com/xiexingen/Bootstrap-SmartF ...
- CSS3新动画效果
CSS3添加了几个动画效果的属性,通过设置这些属性,可以做出一些简单的动画效果而不需要再去借助JavaScript.CSS3动画的属性主要分为三类:transform.transition以及anim ...
- 最近用的到的一些js的常用方法(简单的)
由于新的项目开始了,是使用MVC 5.0 开发的,前端使用了两个主流的框架 UIKIT,Ignite UI(收费) 因为是mvc主要用json来交互,不能避免要对前端脚本进行操作,所以就将能用到的方法 ...
- 使用Maven完成自动化打包并部署到Linux服务器下(Tomcat7)
最近在使用maven,顺便尝试了下tomcat部署.网上找到了很多资料但是都不是最新的,所以贴上比较新的Tomcat7部署代码和配置,方便以后回顾-->测试OK. 1. 首先是配置Tomcat ...
- BZOJ 1211: [HNOI2004]树的计数( 组合数学 )
知道prufer序列就能写...就是求个可重集的排列...先判掉奇怪的情况, 然后答案是(N-2)!/π(d[i]-1)! -------------------------------------- ...
- Java "double字符串转数字"
1.int 表示数字的简单类型(值类型),double 表示数字的双精度类型(值类型), 而Integer和Double类型是一个引用的复杂类型 2.Integer.valueOf(String s ...