您的当前位置:首页正文

LeetCode 3. Longest Substring Wi

2024-12-15 来源:东饰资讯网
class Solution:
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        left = 0
        right = 0
        dic = {}
        maxLen = 1

        if s == None or len(s) == 0:
            return 0

        dic[s[0]] = 0
        while right < len(s) - 1:
            right = right + 1

            pos = None
            if s[right] in dic:
                pos =  dic[s[right]]
                if pos >= left:
                    left = pos + 1

            dic[s[right]] = right
            maxLen = max(maxLen, right - left + 1)

            #print(s[right])
            #print(pos)
            #print(left)
            #print(right)
            #print(maxLen)

        #print("-------")
        print(maxLen)
        #print("-------")
        return maxLen
显示全文