mycode 99.21%
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ maxlen = 0 temp ='' for i in s: if i not in temp: temp += i else: maxlen = max(maxlen,len(temp)) temp = temp[temp.index(i)+1:] + i maxlen = max(maxlen,len(temp)) return maxlen
注意:下面这种方式也可以及时更替最大长度
def lengthOfLongestSubstring(s): l = 0 ls = "" for i in s: if i in ls: ls = ls[ls.index(i)+1:] ls += i if len(ls) > l: #maxres = max(maxres,len(ls)) l = len(ls) return l lengthOfLongestSubstring("abcdvdf")