欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Leetcode刷题记录——面试题48. 最长不含重复字符的子字符串

程序员文章站 2022-07-16 10:21:54
...

Leetcode刷题记录——面试题48. 最长不含重复字符的子字符串

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        length = len(s)
        if length <= 1:
            return length
        newlist = [1]
        maxa = 1
        hashdict = {s[0]:0}
        for i in range(1,length):
            this = s[i]
            if this in hashdict and hashdict[this] != length:
                old = hashdict[this]
                newlist.append(i - old)
                for key in hashdict:
                    if hashdict[key] < old:
                        hashdict[key] = length
                hashdict[this] = i
            else:
                hashdict[this] = i
                newlist.append(newlist[-1] + 1)
                maxa = max(newlist[-1],maxa)
        return maxa