20200329-剑指offer-面试题48. 最长不含重复字符的子字符串(滑动窗口)
程序员文章站
2022-07-16 09:51:00
...
传送门
借鉴的评论区的思路,滑动窗口
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
maxx = 1
if len(s) == 0:
return 0
# 双指针
lcur = 0
rcur = 1
size = 1
while size >= 1:
if rcur < len(s):
if s.find(s[rcur], lcur, rcur) == -1:
size = rcur - lcur + 1
maxx = max(size, maxx)
else:
for i in range(rcur - 1, lcur - 1, -1):
if s[i] == s[rcur]:
lcur = i + 1
size = rcur - lcur + 1
break
rcur = rcur + 1
else:
rcur = min(rcur, len(s)-1)
maxx = max(maxx, rcur - lcur + 1)
break
return maxx
推荐阅读