题解 | 最长不含重复字符的子字符串

最长不含重复字符的子字符串

https://www.nowcoder.com/practice/48d2ff79b8564c40a50fa79f9d5fa9c7

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @return int整型
#
class Solution:
    def lengthOfLongestSubstring(self , s: str) -> int:
        # write code here
        n, l, cs, ans = len(s), 0, set(), 1#字符串长度,左下标,滑动窗口,最长长度
        for r in range(n):#右下标不断变化
            while s[r] in cs:#左下标及滑动窗口变化
                cs.remove(s[l])
                l += 1
            cs.add(s[r])#滑动窗口内增加新元素
            ans = max(ans,r-l+1)#寻找最大长度
        return ans

全部评论

相关推荐

LastWh1spe...:ssob真有些人和那个没睡醒一样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务