当前位置: 首页 > 后端技术 > Python

力扣3.无重复字符的最长子串

时间:2023-03-26 19:43:12 Python

思路是用一个idx表示当前子串类的起始位置解决方法:deflengthOfLongestSubstring(self,s:str)->int:m=dict()'''对每个charc如果没有出现继续出现:在当前词中字符串索引之前:继续之后:映射位置之后的一位'''idx=0res=0fori,cinenumerate(s):如果c不在m中:通过else:如果m[c]int:m=dict()idx=0res=0fori,cinenumerate(s):如果m中的c和m[c]>=idx:res=max(res,i-idx)idx=m[c]+1m[c]=ires=max(res,i-idx+1)返回res