题解 | #最长无重复子数组#
最长无重复子数组
http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
/**
*
* @param arr int整型一维数组 the array
* @return int整型
*/
function maxLength( arr ) {
if (arr.length < 2) {
return arr.length;
}
let windows = {};
let res = 0;
let left = -1;
for (let right = 0; right < arr.length; right++) {
if (windows[arr[right]] != null) {
left = Math.max(left ,windows[arr[right]]);
}
res = Math.max(res, right-left);
windows[arr[right]] = right;
}
return res;
}
module.exports = {
maxLength : maxLength
};