leetcode——28. 实现 strStr()

时间:2019-10-18
本文章向大家介绍leetcode——28. 实现 strStr(),主要包括leetcode——28. 实现 strStr()使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

简单题

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        if needle=='':
            return 0
        if len(needle)>len(haystack):
            return -1
        if len(needle)==len(haystack):
            if needle==haystack:
                return 0
            else:
                return -1
        n=len(needle)
        i=0
        while i<len(haystack)-n+1:
            if haystack[i:i+n]==needle:
                return i
            else:
                i+=1
        return -1
执行用时 :40 ms, 在所有 python3 提交中击败了93.62%的用户
内存消耗 :13.9 MB, 在所有 python3 提交中击败了5.88%的用户
 
                                                                            ——2019.10.18

原文地址:https://www.cnblogs.com/taoyuxin/p/11698230.html