开始用的列表进行保存,发现提交超时,就改用字符串进行存储,发现字符串要比列表的时间复杂度要低;
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
count = 0
s_str = ''
len_num = len(s)
for i in range(len_num):
ss = s[i]
s_str = ss if ss in s_str else s_str + ss
for j in range(len_num - i - 1):
if s[i + j + 1] not in s_str:
s_str += s[i + j + 1]
else:
if len(s_str) > count:
count = len(s_str)
s_str = ''
break
if len(s_str) > count:
count = len(s_str)
return count