数据流中的中位数
2023/2/23小于 1 分钟
数据流中的中位数
题目链接
题目描述
刷题思路
代码实现
/*
* @Description: 【中等】数据流中的中位数
* @Version: Beta1.0
* @Author: 微信公众号:储凡
* @Date: 2021-04-29 21:38:01
* @LastEditors: 微信公众号:储凡
* @LastEditTime: 2021-04-29 21:39:13
*/
// 存储数据
let result = []
export function Insert(num) {
result.push(num)
// 当然,也可以在push之后去排序
}
/**
* 获取数据流的中位数
*/
export function GetMedian() {
// 先排序
result = result.sort((a, b) => a - b)
const len = result.length
// 奇数
if (len % 2 !== 0) {
return result[Math.floor((len - 1) / 2)]
}
else {
// 分别获取低位 和高位
const low = result[Math.floor((len - 2) / 2)]
const high = result[Math.floor((len) / 2)]
// 去平均值
return (high + low) / 2
}
}
一些建议
更新日志
2024/7/29 15:43
查看所有更新日志
5a2b2
-于c0f2d
-于06596
-于9b9e4
-于b0275
-于5f1e1
-于02ab1
-于8de1a
-于d0347
-于3c0e1
-于ced18
-于a23ce
-于80f08
-于