class Solution:
def maxLength(self, arr):
// 解法1 :创建一个字典,返回字典的key
hashmap = {}
for i in range(len(arr)):
if arr[i] not in hashmap:
hashmap[arr[i]] = i
else:
continue
lis1 = [ ]
for j in hashmap.keys():
lis1.append(j)
# return lis1
print(len(lis1))
// 解法2 : 时间复杂度为O(N)
l = []
for i in range(len(arr)):
if arr[i] not in l:
l.append(arr[i])
else:
continue
return len(l)
牛客解法,有时间复杂度限制
class Solution:
def maxLength(self , arr ):
maxlen=0
stack=[] // 创建一个空栈
for i in arr:
while i in stack:
stack.pop(0) //如果在里面,就弹出第一个值
# // print('pop',stack)
stack.append(i)
// print('append',stack)
maxlen=max(maxlen,len(stack))
return maxlen