-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path28_ImplementstrStr.py
More file actions
47 lines (33 loc) · 961 Bytes
/
Copy path28_ImplementstrStr.py
File metadata and controls
47 lines (33 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# coding: utf8
"""
题目链接: https://leetcode.com/problems/implement-strstr/description.
题目描述:
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
"""
class Solution(object):
def strStr_v1(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not needle:
return 0
la = len(haystack)
lb = len(needle)
for i in range(la - lb + 1):
start = i
for j in range(lb):
if haystack[start] != needle[j]:
break
start += 1
if start - i == lb:
return i
return -1