题目描述
给定一个长度为 n 的整数序列,请找出最长的不包含重复的数的连续区间,输出它的长度。
输入格式
第一行包含整数 n。
第二行包含 n 个整数(均在 0∼105 范围内),表示整数序列。
输出格式
共一行,包含一个整数,表示最长的不包含重复的数的连续区间的长度。
数据范围
1≤n≤105
输入样例:
5
1 2 2 3 5
输出样例:
3
算法
(暴力枚举) O(n2)
write here…
时间复杂度
write here…
空间复杂度
write here…
C++ 代码
my code...#include <iostream>
using namespace std;
const int N=10010;
int a[N],cnt[N]; int maxn;
int main(){
int n;
cin>>n;//没有输入因为没有输出
for(int i=0,j=0;i<n;i++)//i在前,j在后
{ cin>>a[i];
cnt[a[i]]++;//维护区间头
while(cnt[a[i]]>1)cnt[a[j++]]--;//有重复时j向前一个,维护区间尾
maxn=max(maxn,i-j+1);
}
cout<<maxn;
}
//唯一好奇的一点的是什么时候能用双指针