# Leetcode 實現strStr()
# 找子串的問題 一遍遍歷就行了 很簡單
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if haystack == "" and needle == "":
return 0
elif haystack == "" and len(needle) > 0:
return -1
elif len(haystack) > 0 and needle == "":
return 0
else:
len_needle = len(needle)
len_haystack = len(haystack)
if len_haystack < len_needle:
return -1
else:
i = 0
while i <= len_haystack - 1:
if haystack[i:i+len_needle] == needle:
return i
else:
i += 1
return -1
LeetCode 實現strStr()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.