import java.util.*;
public class Solution {
/**
*
* @param arr int整型一维数组 the array
* @return int整型
*/
public int maxLength (int[] arr) {
// write code here
HashMap<Integer, Integer> map = new HashMap<>();
int res = 0;
for(int left = 0, right = 0; right < arr.length; right++){
if(map.containsKey(arr[right]))
map.put(arr[right], map.get(arr[right]) + 1);
else
map.put(arr[right], 1);
while(map.get(arr[right]) > 1){
map.put(arr[left], map.get(arr[left])-1);
left++;
}
res = Math.max(res, right - left + 1);
}
return res;
}
}
牛客-TOP101-BM92
最新推荐文章于 2024-07-27 17:56:15 发布