题解 | #滑动窗口的最大值#
滑动窗口的最大值
https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param num int整型一维数组
* @param size int整型
* @return int整型一维数组
*/
function maxInWindows( num , size ) {
// write code here
const res = [];
if(size > num.length || size === 0) return [];
for(let i=0; i<num.length-size+1; i++) {
// 遍历数组,返回滑动数组的最大值
res.push(sliceArrayMax(num.slice(i, i+size)));
}
return res;
}
// 返回数组中的最大值
function sliceArrayMax(arr) {
let max = arr[0];
for(let i=1; i<arr.length; i++) {
if(arr[i]>max) {
max = arr[i];
}
}
return max;
}
module.exports = {
maxInWindows : maxInWindows
};
科大讯飞公司氛围 423人发布