题目描述
给定一个长度为n的整数序列,请找出最长的不包含重复数字的连续区间,输出它的长度。
输入格式
第一行包含整数n。
第二行包含n个整数(均在0~100000范围内),表示整数序列。
输出格式
共一行,包含一个整数,表示最长的不包含重复数字的连续子序列的长度。
数据范围
1≤n≤100000
算法1
双指针;
C++ 代码
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
const int N = 1e6 + 10;
int a[N];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i ++) scanf("%d", &a[i]);
unordered_map<int, int >hash;
int res = 0;
for (int i = 1, j = 1; i <= n; i ++)
{
hash[a[i]] ++;
while(hash[a[i]] >= 2)//除掉重复的;
{
hash[a[j]] --;
j ++;
}
res = max(res, i - j + 1);
}
cout << res;
return 0;
}