zoukankan      html  css  js  c++  java
  • leetcode28 Implement strStr()

     1 """
     2 Implement strStr().
     3 Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
     4 Example 1:
     5 Input: haystack = "hello", needle = "ll"
     6 Output: 2
     7 Example 2:
     8 Input: haystack = "aaaaa", needle = "bba"
     9 Output: -1
    10 """
    11 """
    12 用切片的方法匹配字符
    13 """
    14 class Solution:
    15     def strStr(self, haystack, needle):
    16         for i in range(len(haystack) - len(needle)+1):#bug此处不加1,input如果为两个空字符串,无法输出i
    17             if needle == haystack[i:i+len(needle)]:
    18                 return i
    19         return -1
  • 相关阅读:
    第七周作业
    第六周作业
    第五周作业
    第四周作业
    第三周作业
    第二周作业
    求最大值及下标
    查找整数
    抓老鼠
    第五周作业
  • 原文地址:https://www.cnblogs.com/yawenw/p/12337722.html
Copyright © 2011-2022 走看看