#双指针,如果遇到重复的词,就更新起始指针
1 class Solution: 2 def lengthOfLongestSubstring(self, s): 3 """ 4 :type s: str 5 :rtype: int 6 """ 7 if len(s)<=1:return len(s) 8 aa={s[0]:0} 9 i=0 10 j=1 11 maxlen=0 12 while(j<len(s)): 13 if not s[j] in aa: 14 aa[s[j]]=j 15 else: 16 maxlen=max(maxlen,j-i)#避免abbca这种情形,i已经=2,但查表得到aa['a']=0,因为我们已经能够确定0-2之间有重复 17 i=max(aa[s[j]]+1,i) 18 aa[s[j]]=j 19 j+=1 20 maxlen=max(maxlen,j-i)#最后计算一遍,计算出'abc'这种不会进入else的情形 21 return maxlen