美文网首页
LeetCode 3. Longest Substring Wi

LeetCode 3. Longest Substring Wi

作者: 费城的二鹏 | 来源:发表于2018-11-14 13:53 被阅读6次

    Longest Substring Without Repeating Characters

    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
    

    相关文章

      网友评论

          本文标题:LeetCode 3. Longest Substring Wi

          本文链接:https://www.haomeiwen.com/subject/xwxjfqtx.html