题目描述
给定一个长度为n的整数序列,请找出最长的不包含重复的数的连续区间,输出它的长度。
输入格式
第一行包含整数n。
第二行包含n个整数(均在0~100000范围内),表示整数序列。
输出格式
共一行,包含一个整数,表示最长的不包含重复的数的连续区间的长度。
数据范围
1≤n≤100000
输入样例:
5 1 2 2 3 5
输出样例:
3
Solution
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(in.readLine());
String[] s = in.readLine().split(" ");
int a[] = new int[n + 10];
for (int i=0; i<n; i++){
a[i] = Integer.parseInt(s[i]);
}
int q[] = new int[100010];
int res = 0;
for (int i=0,j=0; j<n; j++){
q[a[j]]++;
while (i<j && q[a[j]] > 1){
q[a[i]] --;
i++;
}
res = Math.max(res,j-i+1);
}
out.write(String.valueOf(res));
out.flush();
}
}