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. JavaScript学习总结(六)

    我们知道,JavaScript共由三部分组成:EMCAScript(基本语法).BOM(浏览器对象模型).DOM. 在浏览器对象模型中,把浏览器的各个部分都用了一个对象进行描述,如果我们要操作浏览器的 ...

  2. c#连接sql server数据库字符串

    第一种方式 Data Source=数据库地址;Initial Catalog=数据库名称;User Id=数据库登录名;Password=数据库密码;[Integrated Security=SSP ...

  3. matlab 高级

    绘图 条形图 x = [1:10]; y = [75, 58, 90, 87, 50, 85, 92, 75, 60, 95]; bar(x,y), xlabel('Student'),ylabel( ...

  4. EXCEL启动慢

    启动太慢,一般是加载项的问题. 1.点击文件-EXCEL选项 2. 找到加载项,一般是COM加载项 3.选择com加载项 4.然后我出现了无法更改的情况,于是,我做了以下调整,进入office安装目录 ...

  5. 题解 P2981 【[USACO10FEB]奶牛在冰Cows on Ice】

    楼上的思路都是从一个石头找跳到另一个石头的路径,但其实这题可以对于上下左右四个方向分别做一个虚拟节点,然后只需要找虚拟节点左边的虚拟节点就行了 问题是:不会用set怎么办??? 其实可以发现用vect ...

  6. nfs 支持ipv6

    mount 一个ipv6 nfs 项目在docker里mount 一个nfs来读写,而现在需要支持ipv6,所以先写了各小demo,最后成功mount,这里记录一下 #include <sys/ ...

  7. ZJNU 1365 - Window--中级

    每次都寻找长度为k的区间内的最小值显然很容易超出时间限制 所以可以把窗户看作一个数量固定的队列 每次观察入列与出列的元素对答案贡献如何,以更新答案 /* Written By StelaYuri */ ...

  8. Python模块——hashlib

    简介 hashlib模块是用于对字符串进行加密,其可以把任意长度的数据转换为一个长度固定的数据串,且这种加密是不可逆的,故这种加密方式的安全性都很高.hash本质是一个函数,该模块提供了许多不同的加密 ...

  9. C语言中“段错误”出现的场景

    1.第一种“段错误”出现的场景 1 /************************************************************************* > Fi ...

  10. day56-mysql-其他查询-面试题-重点

    一.重点 面试题 1. 临时表查询: -- 查询高于本部门平均工资的人员 #1.先查询部门人员的平均工资 SELECT dept_id,AVG(salary)as sal from person GR ...