28,实现strStr

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        # if haystack == needle:
        #     return 0
        # len_n = len(needle)
        # for i in range(len(haystack)):
        #     if haystack[i:i+len_n] == needle:
        #         return i
        # return -1
        try:
            return haystack.index(needle)
        except Exception:
            return -1

猜你喜欢

转载自blog.csdn.net/weixin_42758299/article/details/88630910
今日推荐