题解 | #最长不含重复字符的子字符串#
最长不含重复字符的子字符串
https://www.nowcoder.com/practice/48d2ff79b8564c40a50fa79f9d5fa9c7
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param s string字符串 # @return int整型 # class Solution: def lengthOfLongestSubstring(self , s: str) -> int: # write code here dp = [1 for i in range(len(s))] dic = {} dic[s[0]] = 0 left = 0 right = 0 resl = 0 resr = 0 for j in range(1,len((s))): if s[j] not in dic.keys(): right = j dic[s[j]]=j dp[j] = dp[j-1]+1 if right-left>resr-resl: resl = left resr = right else: orileft = left oriplace = dic[s[j]] left = dic[s[j]]+1 for k in range(orileft,left,1): del dic[s[k]] dic[s[j]]=j right = j dp[j]= right-left+1 if right-left>resr-resl: resl = left resr = right return resr-resl+1