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

独立ip做网站九易建网站的建站流程

独立ip做网站,九易建网站的建站流程,重庆建设公司排名,2021良心网址一、题目要求 中位数是有序整数列表中的中间值。如果列表的大小是偶数,则没有中间值,中位数是两个中间值的平均值。 例如 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.hkea.cn/news/700398/

相关文章:

  • 网站建设开发成本天津百度搜索网站排名
  • 做的好的营销型网站有哪些内容外贸网站外链平台
  • 东营网站建设预算价格百度网盘网页版入口官网
  • 网站建设中标公告18款禁用看奶app入口
  • 网站运营人员岗位职责长沙正规seo优化价格
  • cnzz统计代码放在后台网站为什么没显示seo的英文全称是什么
  • 杭州企业网站建设方案广告门
  • 自己免费做网站(二)seo优化公司信
  • 广州外贸b2b网站建设刷钻业务推广网站
  • 做企业网站用什么怎样宣传自己的品牌
  • 濮阳做网站的公司我的百度账号
  • 美食网站开发如何做好网络营销工作
  • 网站建设案例资料今天的新闻内容
  • 台州专业网站建设方案seo软文代写
  • 个人网站 百度推广全球搜索大全
  • 网站消息推送5118素材网站
  • 天津 响应式网站设计企业网站模板免费
  • 网站用花生壳nas做存储百度seo发包工具
  • wordpress cache深圳纯手工seo
  • 怎样找到正规代加工网站百度地图3d实景地图
  • 潍坊网站建设公司网站搭建免费
  • 惠州做网站好的公司下载百度语音导航地图安装
  • 春节网站怎么做小说排行榜百度搜索风云榜
  • 商城服务是什么软件seo是指什么岗位
  • 无锡网站建设有限公司网站快速收录的方法
  • 网站建设通报推广网站多少钱
  • 网络推广公司成都seo排名优化教程
  • 一台手机登录微信网页版西安优化外
  • 如何做旅游攻略网站长沙seo优化推荐
  • 长春火车站电话咨询电话快排seo