class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if not s:
return 0
current_len = 0
max_len = 0
lookup =set() #存储不重复的字符
for i in range(len(s)):
current_len +=1
if s[i] in lookup:
lookup.remove(s[i])
current_len -= 1
max_len = max(current_len, max_len)
lookup.add(s[i])
return max_len
Leetcode 3. Longest Substring Without Repeating Characters (无重复字符的最长字串)Python
最新推荐文章于 2021-10-15 10:32:34 发布