当前位置: 首页 > news >正文

网站描述wordpresswordpress怎么用panel

网站描述wordpress,wordpress怎么用panel,西安网站建设培训中心,国外大气网站欣赏一、题目要求 中位数是有序整数列表中的中间值。如果列表的大小是偶数,则没有中间值,中位数是两个中间值的平均值。 例如 arr [2,3,4] 的中位数是 3 。 例如 arr [2,3] 的中位数是 (2 3) / 2 2.5 。 实现 MedianFinder 类: MedianFinder() 初始化 …

一、题目要求

中位数是有序整数列表中的中间值。如果列表的大小是偶数,则没有中间值,中位数是两个中间值的平均值。
例如 arr = [2,3,4] 的中位数是 3 。
例如 arr = [2,3] 的中位数是 (2 + 3) / 2 = 2.5 。

实现 MedianFinder 类:
MedianFinder() 初始化 MedianFinder 对象。
void addNum(int num) 将数据流中的整数 num 添加到数据结构中。
double findMedian() 返回到目前为止所有元素的中位数。与实际答案相差 10-5 以内的答案将被接受。

示例 1:
输入
[“MedianFinder”, “addNum”, “addNum”, “findMedian”, “addNum”, “findMedian”]
[[], [1], [2], [], [3], []]
输出
[null, null, null, 1.5, null, 2.0]

解释
MedianFinder medianFinder = new MedianFinder();
medianFinder.addNum(1); // arr = [1]
medianFinder.addNum(2); // arr = [1, 2]
medianFinder.findMedian(); // 返回 1.5 ((1 + 2) / 2)
medianFinder.addNum(3); // arr[1, 2, 3]
medianFinder.findMedian(); // return 2.0
提示:

-105 <= num <= 105
在调用 findMedian 之前,数据结构中至少有一个元素
最多 5 * 104 次调用 addNum 和 findMedian

二、实现代码

原理:使用了两个堆存储数据,一个最大堆用于存储较小的一半元素,另一个最小堆用于存储较大的一半元素,然后根据堆顶元素计算得到中位数。
1. Java

class MedianFinder {private PriorityQueue<Integer> low;private PriorityQueue<Integer> high;public MedianFinder() {// Max-heap to store the smaller half elementslow = new PriorityQueue<>((a, b) -> b - a);// Min-heap to store the larger half elementshigh = new PriorityQueue<>();}public void addNum(int num) {low.offer(num);high.offer(low.poll());if (low.size() < high.size()) {low.offer(high.poll());}}public double findMedian() {if (low.size() > high.size()) {return low.peek();} else {return (low.peek() + high.peek()) / 2.0;}}
}

2. C++

class MedianFinder {
private:priority_queue<int> low; // Max-heappriority_queue<int, vector<int>, greater<int>> high; // Min-heappublic:MedianFinder() { }void addNum(int num) {low.push(num);high.push(low.top());low.pop();if (low.size() < high.size()) {low.push(high.top());high.pop();}}double findMedian() {if (low.size() > high.size()) {return low.top();} else {return (low.top() + high.top()) / 2.0;}}
};

3. Python3

class MedianFinder:def __init__(self):self.low = []  # max-heap (inverted min-heap)self.high = []  # min-heapdef addNum(self, num: int) -> None:heapq.heappush(self.low, -num)heapq.heappush(self.high, -heapq.heappop(self.low))if len(self.low) < len(self.high):heapq.heappush(self.low, -heapq.heappop(self.high))def findMedian(self) -> float:if len(self.low) > len(self.high):return -self.low[0]else:return (-self.low[0] + self.high[0]) / 2.0

:如果四python会出错,只能是python3

http://www.yayakq.cn/news/155230/

相关文章:

  • 电子商务网站建设心得网站需求分析问题
  • 网站模板 阿里西安做网站维护的公司
  • 能下载的网站网站建设技术分享
  • 免费发布产品网站广西建设网查询
  • 榆林国贸网站建设ai做网站步骤
  • 营销型网站建设一般包含哪些内容玩家自助充值网站建设
  • 山西大同最新通告今天厦门seo网站排名优化
  • 郯城建设银行网站wordpress访问需要密码
  • 阿里云 域名 做网站一流的嘉兴网站建设
  • 校园网站建设意义做家教有什么网站
  • 舆情研判分析报告杭州百度快照优化公司
  • 免费网站添加站长统计seo秘籍优化课程
  • php是怎么设计网站的个人响应式网站设计
  • 地图销售网站网站地图 用户体验
  • 建站之星做出的网站不安全wordpress woocommerce 插件
  • 发布外链网站提升学历有哪几种途径含金量高的
  • 玉树州公司网站建设生态文明建设网站专题培训
  • 建设网站需要电脑配置html写手机网站
  • 网页制作app手机版网站seo快速排名优化
  • 大型网站频道的建设需多人协同开发杭州网站建设宣盟网络
  • 北方外贸网站建设cdr平面设计教程
  • wordpress 外贸网站建设网站右侧二维码
  • 自己做网站有什么意义西宁高端网站建设
  • 制作网页的的网站建设化妆品网站的成本
  • 专门做蛋糕面包的网站dw网页制作教程简单
  • 个人可以做电影网站吗网站代码如何导入
  • 建设工程管理网站深圳市宝安区邮编
  • 做外单要上什么网站做体彩网站怎么做
  • 自己做网站怎么盈利沈阳网站推广优化公司
  • 制作专业网站网站团队介绍