class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ usedchar = {} #存储无重复得字符 max_length = start = 0 #初始化最长字符和 for i, c in enumerate(s): if c in usedchar and start <= usedchar[c]: #如果有字符在字典中,并且大于子串起始部分,则从新定位子串的起始位置 start = usedchar[c] + 1 else: max_length = max(max_length, i - start + 1) #取最大值 usedchar[c] = i return max_length