题目来源:

  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()的更多相关文章

  1. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  2. 【LeetCode】028. Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. Java for LeetCode 028 Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. LeetCode 028 Implement strStr()

    题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...

  5. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. leetcode第27题--Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  7. LeetCode记录之28——Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. LeetCode(28)Implement strStr()

    题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if nee ...

  9. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

  10. 028 Implement strStr() 实现 strStr()

    实现 strStr().返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 .例 1:输入: haystack = ...

随机推荐

  1. 解决magento保存产品时耗时很长的问题

    以前我在更新产品属性值(拿price为例)的时候,通常会这样做: foreach($product_ids as $id){ $product = Mage::getModel('catalog/pr ...

  2. 让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET

    让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET 让 QtWebkit 支持跨域CROS 2013-05-23 22:05 450人阅读 评论 ...

  3. 第一次当Uber司机,就拉到漂亮妹纸

    黑马哥的Uber司机端装上很久了,一次活儿也没拉,心里一直有一种当“张师傅”的冲动.黑马哥当Uber司机,肯定不是为了图挣钱,也不是因为Uber有“新约炮神器”的称号,能通过“拉活”来泡妹纸.黑马哥体 ...

  4. 【iOS-Android开发对照】之 数据存储

    [iOS-Android开发对照]之 数据存储 写在前面的话 相比Android和iOS,我认为Android的数据存储更开放一些.Android天生就能够使用多Java I/O:并且天生开放的特性, ...

  5. MVC过滤器基本使用

        Action过滤器 /// <summary> /// 执行代码前执行 /// </summary> /// <param name="filterCo ...

  6. java思考题

    1.仔细阅读示例: EnumTest.java,运行它,分析运行结果? 你能得到什么结论?你掌握了枚举类型的基本用法了吗? public class EnumTest { public static ...

  7. [Jobdu] 题目1521:二叉树的镜像

    不知道怎么回事下面的代码通过了4个测试用例,还有1个测试用例始终是Runtime Error,各位帮我看一下是哪里出了问题 镜像输出两种方法,一种是递归进行调整,另外一种就是直接在先序遍历的基础上进行 ...

  8. 8.PHP 教程_PHP字符串

    字符串变量用于存储并处理文本. PHP中的字符串变量 字符串变量用于包含有字符的值. 在创建字符串之后,我们就可以对它进行操作了.您可以直接在函数中使用字符串,或者把它存储在变量中. 在下面的实例中, ...

  9. 利用python进行数据分析之数据规整化

    数据分析和建模大部分时间都用在数据准备上,数据的准备过程包括:加载,清理,转换与重塑. 合并数据集 pandas对象中的数据可以通过一些内置方法来进行合并: pandas.merge可根据一个或多个键 ...

  10. [LeetCode]题解(python):129-Sum Root to Leaf Numbers

    题目来源: https://leetcode.com/problems/sum-root-to-leaf-numbers/ 题意分析: 一棵树,从跟节点到叶子节点比如1->2那么这个叶子代表12 ...