Implement strStr().

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

Example 1:

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

Example 2:

  1. Input: haystack = "aaaaa", needle = "bba"
  2. 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)
  1. class Solution(object):
  2. def strStr(self, haystack, needle):
  3. """
  4. :type haystack: str
  5. :type needle: str
  6. :rtype: int
  7. """
  8. if not needle:
  9. return 0
  10. if not haystack:
  11. return -1
  12. len_haystack, len_needle = len(haystack), len(needle)
  13. for i in range(0, len_haystack - len_needle + 1):
  14. cur = haystack[i]
  15. if cur == needle[0]:
  16. j = 0
  17. while j < len_needle:
  18. if haystack[i + j] != needle[j]:
  19. break
  20. j += 1
  21. if j == len_needle:
  22. return i
  23. 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. TCP/IP与IETF的RFC

    究竟是谁控制着 TCP/IP协议族,又是谁在定义新的标准以及其他类似的事情?事实上, 有四个小组在负责Internet技术. 1) Internet协会(ISOC,Internet Society)是 ...

  2. 计蒜客 密码锁(BFS)

    https://www.jisuanke.com/course/1797/121114 Description 现在一个紧急的任务是打开一个密码锁.密码由四位数字组成,每个数字从 1 到 9 进行编号 ...

  3. Android开发环境搭建以及模拟环境搭建

    Android开发环境 现在主流的Android开发环境有: Eclipse + ADT + SDK Android Studio + SDK IntelliJ IDEA + SDK 现在国内大部分开 ...

  4. MySQL-SQL语句分类

    MySQL中的SQL语句有:DDL,DML,DCL,DQL,TCL DDL:数据库定义语言 data Definition language 用于创建.修改.和删除数据库内的数据结构,如: 1:创建和 ...

  5. C语言 指针在函数传参中的使用

    int add(int a, int b)   //函数传参的时候使用了int整型数据,本身是数值类型.实际调用该函数时,实参将自己拷贝一份,并将拷贝传递给形参进行运算.实参自己实际是不参与运算的.所 ...

  6. Dinic学习笔记

    网络流是啥不用我说了吧 增广路定理不用我说了吧 Dinic就是分层然后只在层间转移,然后就特别快,\[O(N^2M)\] 伪代码: function dinic int flow = 0 ; whil ...

  7. Python笔记_第一篇_面向过程_第一部分_6.语句的嵌套

    学完条件控制语句和循环控制语句后,在这里就会把“语言”的精妙之处进行讲解,也就是语句的嵌套.我们在看别人代码的时候总会对一些算法拍案叫绝,里面包含精妙和精密的逻辑分析.语句的嵌套也就是在循环体内可以嵌 ...

  8. 代码验证ncut和谱聚类的系数

    W = rand(30); W = W+W'; I = cell(3,1); I{1} = 1:10; I{2} = 11:20; I{3} = 21:30; vol = -ones(3,1); fo ...

  9. winEdt 使用

    晚上摘抄的方法: 1.点选Options -> Options Interface 2.右边会跳出一个介面,点选Advance Configuration... -> Event Hand ...

  10. 吴裕雄--天生自然 PYTHON3开发学习:日期和时间

    import time; # 引入time模块 ticks = time.time() print ("当前时间戳为:", ticks) import time localtime ...