class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
start = 0
rec = {}
cur_len = 0
ans = 0
for end in range(0, len(s)):
cur_len += 1
if s[end] in rec:
start = max(rec[s[end]], start)
rec[s[end]] = cur_len
ans = max(end-start+1, ans)
else:
rec[s[end]] = cur_len
ans = max(end-start+1, ans)
return ans
Leetcode 3. 无重复字符的最长子串
最新推荐文章于 2023-07-20 11:50:43 发布