解法:用set来判断重复,用双指针来确定长度,右指针扩展,左指针缩减至不重复。
import java.util.*;
public class Solution {
/**
*
* @param arr int整型一维数组 the array
* @return int整型
*/
public int maxLength (int[] arr) {
// write code here
if(arr.length<2){
return arr.length;
}
Set<Integer> set = new HashSet<>();
int max = 0;
int left = 0;
int right = 0;
while(right<arr.length){
if(set.contains(arr[right])){
while(set.contains(arr[right])){
set.remove(arr[left++]);
}
}
set.add(arr[right++]);
max = Math.max(max,set.size());
}
return max;
}
}