[LeetCode] 28. Implement strStr()

Success
Details 
Runtime: 36 ms, faster than 80.76% of Python3 online submissions for Implement strStr().
Memory Usage: 13.9 MB, less than 12.31% of Python3 online submissions for Implement strStr().
 

Submission Detail

74 / 74 test cases passed.
Status: 

Accepted

Runtime: 36 ms
Memory Usage: 13.9 MB
Submitted: 0 minutes ago
class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        #0
        if len(needle) ==0:
            return 0
        #normal
        for index in range(len(haystack) - len(needle)+1):
            if haystack[index:index + len(needle)] == needle:
                return index
        #-1 
        return -1

 

16ms:

23333 强烈建议把python踢出语言序列

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

 

posted @ 2019-08-12 10:57  夜歌乘年少  阅读(155)  评论(0编辑  收藏  举报