【Leetcode】28: 实现 strStr()

这个题目有两种比较常用的方法,一种是暴力解法,时间复杂度是O(mn),一种是KMP算法,由于KMP算法一般都是ACM竞赛才会使用到,因此这里这里只提供Python的暴利解法。

题目如下:

 

 

 使用Python暴利解答的代码如下:

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        if len(haystack)==0 and len(needle)==0:
            return 0
        if haystack==None and needle==None:
            return 0
        else:
            if needle in haystack:
                return haystack.index(needle)
            else:
                return -1

 

posted @ 2021-04-20 16:10  Geeksongs  阅读(35)  评论(0编辑  收藏  举报

Coded by Geeksongs on Linux

All rights reserved, no one is allowed to pirate or use the document for other purposes.