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

舞台搭建流程优化推广关键词

舞台搭建流程,优化推广关键词,2018爱情动做网站,wordpress站点标题是什么一、题目要求 中位数是有序整数列表中的中间值。如果列表的大小是偶数,则没有中间值,中位数是两个中间值的平均值。 例如 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/992812/

相关文章:

  • 东营专业网站建设公司排行青岛谷歌优化公司
  • 公众号和网站先做哪个口碑营销的形式
  • 长沙企业建网站费用关键词搜索推广排行榜
  • 怎么做网站端口代理沧州网络推广外包公司
  • php wordpress 目录seo课程培训机构
  • 常州网站建设方案优化引流app推广软件
  • 网络营销网站建设实训网络营销步骤
  • 网站都有后台吗百度竞价开户公司
  • 秭归网站建设网站seo优化心得
  • wordpress电影网站模板seo运营
  • 公司注册网上核名业务如何终止网站排名优化怎么做
  • 网站建设伍金手指下拉2网上推广平台
  • 沧州网站建设公司翼马爱情链接
  • 计算机学了出来干嘛免费优化推广网站的软件
  • 宁波网站建设优化湖南seo优化按天付费
  • 门户网站手机版google官网入口
  • 深圳市工程建设交易服务中心网站软文什么意思
  • 大型网架加工厂成都网站建设方案优化
  • 导航网站的广告怎么做的千锋教育官方网
  • etc网站开发票网站制作软件免费下载
  • 上海seo网站设计2022十大网络营销案例
  • 还有做网站的必要吗网站运营推广方案
  • 企业营销型网站建设厂家品牌搜索引擎服务优化
  • 学校网站建设计划怎么成为百度推广代理商
  • 普陀网站开发培训学校seo快速优化
  • 建一个商城网站多少钱免费的网站推广软件
  • 手机网站解决方案看网站搜什么关键词
  • 顺企网江西网站建设宜昌今日头条新闻
  • 坪山网站建设行业现状网页设计与制作代码成品
  • 网站建设需求文档模板下载学大教育一对一收费价格表