一、题目地址
二、思路地址
https://www.bilibili.com/video/BV1uA411H7nU?from=search&seid=16801098358158986156
三、具体代码
/**
*
* @param arr int整型一维数组 the array
* @return int整型
*/
function maxLength( arr ) {
let map = new Map();
let start = 0;
let end = 0;
let max = 1;
while(true) {
if(end >= arr.length) {
break;
}
let temp = map.get(arr[end]);
//map中还没存在
if(temp === undefined || temp < start) {
max = Math.max(max, end - start + 1);
}else {//map中已经存在了
start = temp + 1;
}
map.set(arr[end], end);
end++;
}
return max;
}
module.exports = {
maxLength : maxLength
};