剑指 Offer 48. 最长不含重复字符的子字符串

原题链接

begin为最长不含重复字符的子字符串的起点

1 class Solution:
2     def lengthOfLongestSubstring(self, s: str) -> int:
3         begin,ans,dic = 0,0,{}
4         for index,c in enumerate(s):
5             if c in dic:
6                 begin = max(dic[c] + 1,begin) 
7             dic[c] = index #更新字符c的坐标为当前坐标
8             ans = max(index - begin + 1, ans)
9         return ans

 

posted @ 2021-01-24 12:33  凝视深空  阅读(48)  评论(0编辑  收藏  举报