https://www.acwing.com/problem/content/description/801/
给定一个长度为 n 的整数序列,请找出最长的不包含重复的数的连续区间,输出它的长度。
第一行包含整数 nn。
第二行包含 n 个整数(均在 0∼10^5范围内),表示整数序列。
共一行,包含一个整数,表示最长的不包含重复的数的连续区间的长度。
1≤n≤10
5
1 2 2 3 5
3
#include<iostream>
using namespace std;
const int N = 100005;
int q[N], f[N]; // q[N]为数组,f[N]记录出现次数
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &q[i]);
int res = 0;
for (int i = 0, j = 0; i < n; i++) {
f[q[i]]++;
while (j < i && f[q[i]] > 1) f[q[j ++]] --;
res = max(res, i - j + 1);
}
printf("%d", res);
}