题解 | #最长无重复子数组#
最长无重复子数组
https://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param arr int整型一维数组 the array
* @return int整型
*/
function maxLength(arr) {
// write code here
if (arr.length < 2) return arr.length;
let p1 = 0;
let p2 = 0;
let res = 1;
for (p2 = 1; p2 < arr.length; p2++) {
if (!arr.slice(p1, p2).includes(arr[p2])) {
continue;
} else {
res = Math.max(res, p2 - p1);
p1 = arr.slice(p1, p2).indexOf(arr[p2]) + p1+1;
console.log("p1", p1);
}
}
res = Math.max(res, p2 - p1);
console.log(res);
return res;
}
module.exports = {
maxLength: maxLength,
};

查看22道真题和解析