题解 | #最长无重复子数组#
最长无重复子数组
http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
#
#
# @param arr int整型一维数组 the array
# @return int整型
#
class Solution:
def maxLength(self , arr ):
# write code here
'''
# 方法一:队列
Max, queue = 0, []
for n in arr:
while n in queue:
queue.pop(0)
queue.append(n)
Max = max(len(queue), Max)
return Max
'''
# 方法二:滑动窗口
j, Max, seen = 0, 0, set()
for i in range(len(arr)):
while j < len(arr) and arr[j] not in seen:
seen.add(arr[j])
j += 1
Max = max(Max, j-i) # 发现重复的,统计最大长度
seen.remove(arr[i]) # 删除重复的元素
return Max