给定一个长度为n的整数序列,请找出最长的不包含重复数字的连续区间,输出它的长度。
第一行包含整数n。
第二行包含n个整数(均在0~100000范围内),表示整数序列。
共一行,包含一个整数,表示最长的不包含重复数字的连续子序列的长度。
1≤n≤1000001≤n≤100000
5
1 2 2 3 5
3
#include<iostream> #include<algorithm> using namespace std; const int N = 1e5+ 10; int a[N],s[N]; int main(){ int n; cin >> n; for(int i = 0;i < n;i++) cin >> a[i]; int res = 0; for(int i = 0,j= 0;i< n;i++){ s[a[i]] ++; while(s[a[i]] > 1){ s[a[j++]] --;//减到s[a[i]] 小于一 } res = max(res,i - j + 1); } cout << res; }
原文:https://www.cnblogs.com/luyuan-chen/p/11909280.html