一、题目
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
二、示例

提示
- 0 <= s.length <= 5 * 104
- s 由英文字母、数字、符号和空格组成
三、解法
解法-滑动窗口
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
n = len(s)
if n < 2:
return n
i = 0
res = -1
tempList = []
while i < n:
temp = s[i]
index = ''.join(tempList).find(temp)
if temp != -1:
res = max(res,len(tempList))
tempList = tempList[index+1:]
i += 1
tempList.append(temp)
res = max(res,len(tempList))
return res
四、思路
用tempList来储存当前的不含有重复字符的子串,用res储存最大子串的长度。
注意退出循环后,需要再进行一次res判定。
|