当前位置:网站首页>LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
2022-07-03 09:20:00 【三岁就很萌@D】
class MedianFinder {
/** initialize your data structure here. */
private PriorityQueue<Integer> minHeap;//小顶堆
private PriorityQueue<Integer> maxHeap;//大顶堆
public MedianFinder() {
minHeap = new PriorityQueue<Integer>(new Comparator<Integer>(){
public int compare(Integer i1,Integer i2){
return i1.compareTo(i2);
}
});
maxHeap = new PriorityQueue<Integer>(new Comparator<Integer>(){
public int compare(Integer i1,Integer i2){
return i2.compareTo(i1);
}
});
}
public void makeBalance(){
if(minHeap.size() > maxHeap.size()){
maxHeap.offer(minHeap.poll());
}
else if(maxHeap.size() > minHeap.size() + 1){
minHeap.offer(maxHeap.poll());
}
}
public void addNum(int num) {
if(maxHeap.size() == 0 || num <= maxHeap.peek())
maxHeap.offer(num);
else
minHeap.offer(num);
makeBalance();
}
public double findMedian() {
int k = minHeap.size() + maxHeap.size();
return k % 2 == 0 ? ((double) minHeap.peek() + maxHeap.peek()) / 2 : maxHeap.peek();
}
}
/** * Your MedianFinder object will be instantiated and called as such: * MedianFinder obj = new MedianFinder(); * obj.addNum(num); * double param_2 = obj.findMedian(); */
边栏推荐
- Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?
- Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
- A lottery like scissors, stone and cloth (C language)
- My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
- My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
- is_ power_ of_ 2 judge whether it is a multiple of 2
- Windows下MySQL的安装和删除
- Swing transformer details-2
- Uniapp realizes global sharing of wechat applet and custom sharing button style
- JS基础-原型原型链和宏任务/微任务/事件机制
猜你喜欢
Stm32-hal library learning, using cubemx to generate program framework
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
ADS simulation design of class AB RF power amplifier
要选择那种语言为单片机编写程序呢
LeetCode - 673. 最长递增子序列的个数
Schematic diagram and connection method of six pin self-locking switch
Project cost management__ Cost management technology__ Article 6 prediction
2.Elment Ui 日期选择器 格式化问题
Windows下MySQL的安装和删除
2021-10-27
随机推荐
03 FastJson 解决循环引用
一个可执行的二进制文件包含的不仅仅是机器指令
Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
STM32 running lantern experiment - library function version
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
Working mode of 80C51 Serial Port
Stm32f04 clock configuration
Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
LeetCode - 919. 完全二叉树插入器 (数组)
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
How does the memory database give full play to the advantages of memory?
2021-10-28
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
Crash工具基本使用及实战分享
使用sed替换文件夹下文件
STM32 general timer 1s delay to realize LED flashing
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
byte alignment