返回题解分享
讨论 / 题解分享/ 帖子详情

最长连续不重复子区间 - 题解

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N];
int cnt[N];
int n;
int main(){
    scanf("%d",&n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d",&a[i]);
    }
    int res = -1;
    for (int i = 1,j = 1; i <= n; ++i) {
        cnt[a[i]]++;
        while(j <= i && cnt[a[i]] > 1) {
            cnt[a[j]]--;
            ++j;
        }
        res = max(res,i - j + 1);
    }
    printf("%d\n",res);
    return 0;
}
0 回复 0 转发 0 喜欢 2 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!