LeetCode28

LeetCode28:https://leetcode-cn.com/problems/implement-strstr/submissions/

解题思路:滑动窗法

1 class Solution:
2     def strStr(self, haystack: str, needle: str) -> int:
3         len1 = len(haystack)
4         len2 = len(needle) 
5         for i in range(len1-len2+1):
6             if haystack[i:i+len2] == needle:
7                 return i
8         return -1

 

posted @ 2021-02-18 16:52  vv_869  阅读(68)  评论(0编辑  收藏  举报