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. php速成_day4

    一.微信公众平台概述 1.微信发展史 1)2011年1月21日,腾讯推出微信应用程序.(张小龙) 2)2012年8月20日,腾讯推出微信公众平台功能,同年11月开放第三方接口 3)2013年11月注册 ...

  2. 云托管,边缘物理计算&托管物理计算,你所需要了解的……

    随着业务发展,传统数据中心建设复杂性越来越高,基建的管理.设备的繁杂.人力成本的提升,是否让你的运维成本越来越高?企业生产效率却越来越低? 业务快速发展,设备采购周期冗长,大量采购造成CAPEX过重, ...

  3. MySQL--mysqldump(数据导出工具)

    mysqldump 客户端工具用来备份数据库或在不同数据库之间进行数据迁移.备份内容包含创建表或装载表的 SQL 语句.mysqldump 目前是 MySQL 中最常用的备份工具. 有 3 种方式来调 ...

  4. sqlserver修改某列为自增

    sqlserver如果建表的时候不设自增,之后是没法直接修改的,需要先删再重设: alter table 表名 drop column ID alter table 表名 add ID int ide ...

  5. 并发与高并发(三)-CPU多级缓存の乱序执行优化

    一.CPU多级缓存-乱序执行优化 处理器或编译器为提高运算速度而做出违背代码原有顺序的优化. 重排序遵循原则as-if-serial as-if-serial语义:不管怎么重排序(编译器和处理器为了提 ...

  6. redis备忘录

    Redis 是一个基于内存的高性能key-value数据库.Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载在内存当中进行操作,定期通过异步操作把数 ...

  7. [前端] VUE基础 (6) (v-router插件、获取原生DOM)

    一.v-router插件 1.v-router插件介绍 v-router是vue的一个核心插件,vue+vue-router主要用来做SPA(单页面应用)的. 什么是SPA:就是在一个页面中,有多个页 ...

  8. Java常见异常说明汇总

    1. java.lang.nullpointerexception 这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存在的对 ...

  9. FastReport 使用入门 (二)

    上部分  我们将格式大概都画好了 下面 我们将Datatable的每列绑定到  我们添加的table控件上 .然后打开table控件的事件 双击选中 ManualBuild 事件 添加代码 priva ...

  10. 通过if语句实现for循环的提前结束

    /************************************************************************* > File Name: mybreakin ...