输入数据:
5
1 2 2 3 5
第一轮:
i = 0, j = 0, a[0] = 1, s[1] = 1, while循环不成立,res = max(0,0 - 0 + 1) = 1;
第二轮:
i = 1, j = 0, a[1] = 2, s[2] = 1, while循环不成立,res = max(1,1 - 0 + 1) = 2;
第三轮:
i = 2, j = 0, a[2] = 2, s[2] = 2, while循环成立:
s[a[0]]--,s[a[0]] = 0, // 相当于a[0] = 1不满足条件,需要去掉
j++, 最左指针右移
s[a[1]]--,s[a[1]] = 1, // 去掉第一个2
j++, 右移
此时,s[a[i]] = s[2] = 1,跳出while,j = 2
第四轮:
i = 3,j = 2, a[3] = 3, s[3] = 1, while循环不成立,res = max(2,3 - 2 + 1) = 2;
第五轮:
i = 4,j = 2, a[4] = 5, s[5] = 1, while循环不成立,res = max(2,4 - 2 + 1) = 3;
结束:res = 3