class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
max=0
newstr=''
for i in range(len(s)):
if s[i] not in newstr:
newstr +=s[i]
else:
a=newstr.index(s[i])
lens=len(newstr)
if max<=lens:
max=lens
newstr= newstr[a+1:]+s[i]
if max ==0 and newstr !='':
return len (newstr)
if max >=len(newstr):
return max
else:
return len (newstr)
接替思路:
定义一个新的字符串,用来存放子字符串
遍历字符串
如果遇到重复的,则记录下此刻的最长子串的长度,(记录到max中)
子串通过切片,去除重复的后,继续进行查找