#import itertools as it
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
mlen=0
n=len(s)
for i in range(n):
zd={} #字典比list运算快许多
for ele in s[i:]:
if ele not in zd:
zd[ele]=1
else:
break
length=len(zd)
#print(length)
if length >mlen:
mlen=length
return mlen
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
output=0
track=''#用str的运算速度也比list快
for c in s:
if c in track:
while True: ####从左边开始判断重复,确定新的起点
if track[0]==c:
track=track[1:]
track+=c
break
else:
track=track[1:]
else:
track+=c
if len(track)>output:
output=len(track)
return output
class Solution(object):
def lengthOfLongestSubstring(self, s):
track, output, start = {}, 0, 0
for i, v in enumerate(s):
if v not in track or track[v] < start: #v不在track中或者在开始计数的字符之前
output = max(output, i - start + 1)
else:
start = track[v] + 1 #更新开始计数的初始字符位置
track[v] = i
return output