题解 | #最长无重复子数组#
最长无重复子数组
https://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
import java.util.*;
public class Solution {
/**
*
* @param arr int整型一维数组 the array
* @return int整型
*/
public int maxLength (int[] arr) {
// 用map存储已存在数据
HashMap<Integer,Integer> map = new HashMap();
// 采用动态区间存储,count为hashmap数据类型个数
int i = 0,j = 0;
int count = 0;
int max = 0;
while(j < arr.length){
// 若不存在,count++,并加入map
if(!map.containsKey(arr[j])){
map.put(arr[j],1);
count++;
// 如果存储个数超过最大值,更改max
if(count > max){
max = count;
}
}else{
// 若存在,i后移并删除对应map
while(arr[i] != arr[j]){
map.remove(arr[i]);
count--;
i++;
}
// 再将i进一位保证无重复元素
i++;
}
j++;
}
return max;
}
}
查看11道真题和解析