
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
# 滑动窗口
if not len(s):
return 0
# 最大就定义极小,最小就定义极大
res = float('-inf')
i = 0
mark = set()
for j in range(len(s)):
# 这里不能用if, 应该用while
# if s[j] in mark:
while i<=j and s[j] in mark:
mark.remove(s[i])
i += 1
res = max(res, j-i+1)
mark.add(s[j])
return res if res != '-inf' else 0
这段代码实现了一个Python类Solution,包含lengthOfLongestSubstring方法,用于找出给定字符串s中最长的无重复字符的子串长度。方法采用滑动窗口策略,维护一个集合mark来检查当前窗口内的字符是否重复,通过移动窗口边界找到最长子串。
218

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



