AcWing 799. 最长连续不重复子序列
原题链接
简单
作者:
Broken_
,
2024-09-25 16:31:47
,
所有人可见
,
阅读 1
java 代码
import java.io.BufferedInputStream;
import java.util.Scanner;
import static java.lang.Math.max;
/**
* 双指针算法
* 求 最长了连续不重复子序列
*/
public class Main {
private static final int N = 100010;
private static int[] a = new int[N];//原始数组
private static int[] b = new int[N];//记录每个数出现的次数
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int n = sc.nextInt();
for (int i = 0; i < n; i++) a[i] = sc.nextInt();
int res = 0;
for (int i = 0, j = 0; j < n; j++) {
b[a[j]]++;
while (b[a[j]] > 1) {
b[a[i]]--;
i++;
}
res = max(res, j - i + 1);
}
System.out.println(res);
}
}