LeetCode in Python-28. Implement strStr() 實現strStr()

Implement strStr 實現strStr

題目

在這裏插入圖片描述

解法1、

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        for i in range(len(haystack) - len(needle) + 1):
            if haystack[i:i+len(needle)] == needle:
                return i
        return -1

出處

1、對應題目下Knife丶的題解

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章