Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().

 
Time: O(M * N)
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not needle:
return 0
if not haystack:
return -1
len_haystack, len_needle = len(haystack), len(needle)
for i in range(0, len_haystack - len_needle + 1):
cur = haystack[i]
if cur == needle[0]:
j = 0
while j < len_needle:
if haystack[i + j] != needle[j]:
break
j += 1
if j == len_needle:
return i
return -1

[LC] 28. Implement strStr()的更多相关文章

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

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

  2. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  3. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...

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

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

  5. C# 写 LeetCode easy #28 Implement strStr()

    28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...

  6. [LeetCode] 28. Implement strStr() 实现strStr()函数

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

  7. 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()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  9. 【LeetCode】28 - Implement strStr()

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

随机推荐

  1. 实现3d效果

    transform-origin: center left 60px;可以实现3d转换

  2. 学习SEO之7天精通SEO

    这本书大致看了一下,对于SEO基本上有了一个初步的认识,附上链接以供学习之用. 百度网盘:https://pan.baidu.com/s/1Bntzh2YF4tBd2AYAL1Q8vQ 心得:1.SE ...

  3. Java中常用的API(三)——缓冲区字符串

    前两节中分别介绍了Object和String,这一节主要介绍StringBuffer和StringBuilder. StringBuffer 由于String是不可变的,所以导致String对象泛滥, ...

  4. 加速软件源更新和安装 ubuntu 软件中心

    Linux mint 12 修改加速软件源更新和安装 ubuntu 软件中心 由于 linux mint 12 是基于 ubuntu 的,可以使用 ubuntu 的源(Ubuntu 11.10 代号 ...

  5. uni-app真机调试报错request:fail abort解决方法

    Android端真机调试访问本地接口数据时报错:request:fail abort 报错代码 onLoad: function(e) { uni.request({ url: 'http://loc ...

  6. POJ - 1061 扩展欧几里德算法+求最小正整数解

    //#pragma comment(linker, "/STACK:1024000000,1024000000") //#pragma GCC optimize(2) #inclu ...

  7. 吴裕雄--天生自然 PHP开发学习:MySQL 插入多条数据

    <?php $servername = "localhost"; $username = "root"; $password = "admin& ...

  8. SVN提交时忽略不必提交的文件夹和文件,如node_modules

    空白处右键>选中TortoiseSVN>设置(settings)>常规设置(General)>Subversion>编辑(edit)>在弹出的config文件中找g ...

  9. 【C#并发】00概述

    摘自<C#并发编程经典实例>[美]Stephen Cleary 并发:同时做多件事情.终端用户利用并发功能,在输入数据库的同时相应用户输入.服务器应用并发,在处理第一个请求的同时响应第二个 ...

  10. tensorflow slim训练以及到安卓部署教程

    https://blog.csdn.net/chenyuping333/article/details/81537551 https://blog.csdn.net/u012328159/articl ...