双指针模板题
题目链接
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
const int N = 1e5+7;
int a[N], s[N];
int main(){
int n, res = 0;
cin>>n;
for(int i = 0; i < n; i++){
cin>>a[i];
}
for(int i = 0, j = 0; i < n; i++){
s[a[i]]++; //重复的数字是由当前插入的值引起的
while(s[a[i]] > 1){ //j一直往i移动,直到当前值只出现一次为止
s[a[j]]--;
j++;
}
res = max(res, i - j + 1);
}
cout<<res<<endl;
return 0;
}