import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 滑动窗口,双指针
*
* @param arr int整型一维数组 the array
* @return int整型
*/
public int maxLength (int[] arr) {
int max = 0;
int left = 0;
int right = 0;
Set<Integer> set = new HashSet<>();
// 不超过最大长度继续
while (right < arr.length) {
int num = arr[right];
// 重复了,则移除首相
while (set.contains(num)) {
set.remove(arr[left]);
// 缩小窗口
left++;
}
// 未重复,添加结果
set.add(arr[right]);
// 更新最大值
max = Math.max(set.size(),max);
// 扩大窗口
right++;
}
return max;
}
}