共计 357 个字符,预计需要花费 1 分钟才能阅读完成。
传入两个有序数组,并返回中位数
function getAllMid(arr1, arr2) {const result = [];
let p1 = arr1.length - 1;
let p2 = arr2.length - 1;
while (p1 >= 0 && p2 >= 0) {arr1[p1] > arr2[p2] ? result.push(arr1[p1--]) : result.push(arr2[p2--]);
}
while (p1 >= 0) {result.push(arr1[p1--]);
}
while (p2 >= 0) {result.push(arr1[p2--]);
}
const p = result.length;
if (p % 2) {return result[(p - 1) / 2];
} else {return (result[p / 2 - 1] + result[p / 2]) / 2;
}
}
正文完
发表至: javascript
2021-06-17