给定一个长度为n的整数序列,请找出最长的不包含重复数字的连续子序列,输出它的长度。
输入格式
第一行包含整数n。
第二行包含n个整数(均在0~100000范围内),表示整数序列。
输出格式
共一行,包含一个整数,表示最长的不包含重复数字的连续子序列的长度。
数据范围
1≤n≤100000
输入样例:
5
1 2 2 3 5
输出样例:
3注意这里是连续,算法与LeetCode第八题类似算法:双指针,哈希。
#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
using namespace std;
int main(
void){
int n,res=
0,j=
0;
cin>>
n;
vector<
int>
f(n);
unordered_map<
int,
int>
h;
for(
int i=
0;i<n;i++)cin>>
f[i];
for(
int i=
0;i<n;i++
){
h[f[i]]++
;
while(j<=i&&h[f[i]]>
1)h[f[j++]]--
;
res=max(res,i-j+
1);
}
cout<<res<<
endl;
return 0;
}
转载于:https://www.cnblogs.com/programyang/p/11152310.html