题解 | #最长无重复子数组#
最长无重复子数组
http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
import java.util.*;
public class Solution {
/**
*
* @param arr int整型一维数组 the array
* @return int整型
*/
public int maxLength (int[] arr) {
// write code here
// Queue<Integer> queue = new LinkedList<>();
// int res = 0;
// for (int i = 0; i < arr.length; i++){
// 删除到不包括为止。得到一个不包含重复元素的新数列
// while(queue.contains(arr[i])){
// queue.poll();
// }
// queue.add(arr[i]);
// res = Math.max(res, queue.size());
// }
// return res;
HashSet set = new HashSet();
int res = 0;
int left = 0, right = 0;
while(right < arr.length){
//删除到不包括为止
while(set.contains(arr[right])){
set.remove(arr[left++]);
}
set.add(arr[right++]);
res = Math.max(res, right - left);
}
return res;
}
}
顺丰集团工作强度 316人发布