无重复字符的最长子串 python
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
输入: s = “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
class Solution:
def length_of_substr(self,s:str)->int:
n=len(s)
occ=set()
rk,ans=-1,0
for i in range(n):
if i!=0:
occ.remove(s[i-1])
while rk+1<n and s[rk+1] not in occ:
occ.add(s[rk+1])
rk+=1
ans=max(ans,rk-i+1)
return ans
1187

被折叠的 条评论
为什么被折叠?



