力扣 3. 无重复字符的最长子串

哈希表 + 双指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if not s: return 0
res, bi, left, right = 1, set(), 0, 0
bi.add(s[0])
for i in range(1, len(s)):
right += 1
if s[i] in bi:
while left <= right:
if s[left] != s[i]:
bi.remove(s[left])
left += 1
else:
left += 1
break
bi.add(s[i])

res = max(right - left + 1, res)
return res
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信