21
5 月
2020
leetcode第三题解法2
原题https://leetcode.com/problems/longest-substring-without-repeating-characters/submissions/
最长子窜
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
st = {}
i=0
max1 = 0
for j in range(len(s)):
if s[j] in st:
i = max(st[s[j]], i)#起始位置换到原始重复位置的后一位,如ab遇到a就变成ba去继续遇下一位
max1 = max(max1, j - i + 1)
st[s[j]] = j+1
return max1