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. 2.0 虚拟机linu开启ssh服务与FTP

    2.1.1.当本地机器ssh连接过一次虚拟主机.虚拟主机重启过或者配置发生改变 需要重新配对密钥,需要先清除本地缓存的密钥  ssh-keygen -R "ip"  2.1.2. ...

  2. zabbix使用短信猫实现报警

    因为公司运维的对象是政府单位,所以在实际的监控过程中无法连接到外网,所以最后报警选择的媒介是短信猫,下边就是具体的实施过程. 一.面临的问题 因为手头上的设备是串口的短信猫,但是zabbix serv ...

  3. for-each用法误区(不能改变数组元素值)

    代码例程: /**  * 数据加密传输  */ import java.util.Scanner; public class secretPass {     public static void m ...

  4. C - Monitor CodeForces - 846D (二维前缀和 + 二分)

    Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started ...

  5. 后台用Hbase对表单数据实现增删改查遇到的问题

    1.无法解析jsp 原因:hbase中lib下jar包会与tomcat包冲突,需要删除与tomcat冲突的包 这是我删除的几个包 之后运行就没有问题了 2.对于Hbase修改的问题 在添加数据时,HB ...

  6. latex学习笔记----基本知识、文档排版

    1.空格和制表符等空白字符视为相同的空白距离,多个连续的空白字符等同为一个字符. 2.#  $  %  ^  _    {   }  ~ 在这些字符前面加上反斜线,就可以在文本中得到它们. 反斜线\不 ...

  7. Codeforces 1294C - Product of Three Numbers

    题目大意: 给定一个n,问是否存在3个互不相同的,大于等于2的整数,满足a*b*c=n 解题思路: 可以把abc其中任意两个看作一个整体,例如a*b=d,那么可以发现d*c=n 所以d和c是n的因子 ...

  8. tensorflow object detection api android

    https://blog.csdn.net/weixin_40355324/article/details/80651350

  9. keras中保存自定义层和loss

    在keras中保存模型有几种方式: (1):使用callbacks,可以保存训练中任意的模型,或选择最好的模型 logdir = './callbacks' if not os.path.exists ...

  10. msgfmt - 翻译汉化

    说明 目前大部分自由软件实现国际化使用的是gettext. 国际化就是让程序可以使用多国语言来显示程序里的字符串. 程序里一般都有很多字符串,菜单名也好,错误信息也好,都是字符串.假设字符串为stri ...