Kattis Problem-Unique Snowflakes
题目类型:双指针
题意
给定一个整数序列,找出最长的不重复子串的长度。
分析
使用双指针,枚举每个指针 I,然后每次判断指针 j 最远可以指到那里。
代码
public static void solve() throws IOException {
int n = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int ans = 0;
for (int i = 0, j = 0; i < n; i++) {
map.merge(a[i], 1, (o1, o2)->(o1 + o2));
while (map.get(a[i]) > 1) {
map.merge(a[j], 1, (o1, o2)->(o1 - o2));
j++;
}
ans = Math.max(ans, i - j + 1);
}
pw.println(ans);
}