一开始的思路:
两次嵌套循环把整个列表遍历:
def has_elem(a,s):
for k in a:
if k==s:
return True
return False
class Solution(object):
def lengthOfLongestSubstring(self,s):
lengmax = 0
length = [0]
for i in range(0,len(s)):
a=[]
if (len(s)-i)<lengmax:
break
for j in range(i,len(s)):
if (not has_elem(a,s[j])):
a.append(s[j])
length[i]+=1
else:
if length[i]>lengmax:
lengmax = length[i]
length.append(0)
break
if j == len(s)-1:
if length[i]>lengmax:
lengmax = length[i]
length.append(0)
break
return lengmax
结果运算时间超出了限制。决定换一种思路
建立一个字典,然后将字母作为属性,后续再写