AcWing 799. 最长连续不重复子序列 Java
原题链接
简单
作者:
leo_0
,
2021-07-21 13:25:32
,
所有人可见
,
阅读 143
算法1
Java 代码
import java.util.*;
import java.io.*;
public class Main {
static int[] nums;
final static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
final static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
String[] str = reader.readLine().split("\\s+");
int n = Integer.parseInt(str[0]);
nums = new int[n];
str = reader.readLine().split("\\s+");
int res = 0;
int left = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < n; i++){
nums[i] = Integer.parseInt(str[i]);
if(map.containsKey(nums[i])){
left = Math.max(left, map.get(nums[i])+1);
}
map.put(nums[i], i);
res = Math.max(res, i-left+1);
}
System.out.print(res);
}
}