算法1 双指针算法
时间复杂度$O(n)$
python 代码
if __name__=="__main__":
n = int(input())
num = [int(i) for i in input().split()]
dict = {}
i,m = 0,0
for j,x in enumerate(num):
if x in dict:
i = max(dict[x],i)#维护起点i
m = max(m,j-i+1)
dict[x] = j+1#维护字典全部从1开始算
print(m)
5
1 2 2 3 5
3