便利蜂前端校招笔试题
输入:
nums='100,300,50,30,500,103,605,720'
k=3
输出:[ 300, 300, 500, 500, 605, 720 ]
大概就是 [100,300,50]找最大值是300;[300,50,30]最大值300,[50,30,500]最大值是500.。。。。
让用O(n)的时间复杂度完成
nums = "100,300,50,30,500,103,605,720".split(',');
k=3;
let result=[];
// for循环 O(n)时间复杂度
for(let i=0;i<=nums.length-k;i++) {
max = Math.max(...nums.slice(i,k+i)); //slice是根据数组下标来截取,应该是O1时间复杂度吧
result.push(max);
}
console.log(result);
浙公网安备 33010602011771号