无重复字符的最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
def lengthOfLongestSubstring(s):
length = len(s)
if length < 2:
return length
i = 0
j = 1
char_set = set(s[0])
ss_l = 1
while j < length:
if s[j] not in char_set:
char_set.add(s[j])
j += 1
else:
ss_l = max(ss_l, j - i)
while s[i] != s[j]:
char_set.remove(s[i])
i += 1
i += 1
j += 1
if j == length:
ss_l = max(ss_l, j - i)
return ss_l

浙公网安备 33010602011771号