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

【Hot100】LeetCode—295. 数据流的中位数

目录

  • 1- 思路
    • 题目识别
    • 堆实现
  • 2- 实现
    • 4. 寻找两个正序数组的中位数——题解思路
  • 3- ACM 实现


  • 原题链接:295. 数据流的中位数

1- 思路

题目识别

  • 识别1 :实现一个数据结构,求中位数

堆实现

思路

  • 利用优先队列,小根堆放较小的元素。
  • 大根堆放较大的元素。
  • 例如 1 2 3 4 5 6 ,其中大根堆放的元素是 1 2 3,小根堆放的元素是 4 5 6。中位数就是 (3+4)/2

实现

  • ① 当是偶数
    • 先入小根堆,之后大根堆添加小根堆堆顶元素。
  • ② 当是奇数
    • 先入大根堆,之后小根堆添加大根堆堆顶元素。

2- 实现

4. 寻找两个正序数组的中位数——题解思路

在这里插入图片描述

class MedianFinder {

    Queue<Integer> min;
    Queue<Integer> max;
    public MedianFinder() {
        min = new PriorityQueue<>((x,y) -> (y-x));
        max = new PriorityQueue<>();
    }
    
    public void addNum(int num) {
        if(min.size() == max.size()){
            min.add(num);
            max.add(min.poll());
        }else{
            max.add(num);
            min.add(max.poll());
        }
    }
    
    public double findMedian() {
        if(min.size() == max.size()){
            return (min.peek()+max.peek())/2.0;
        }else{
            return max.peek()*1.0;
        }
    }
}

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder obj = new MedianFinder();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */

3- ACM 实现

package Daily_LC.Month9_Week3.Day157;

import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

/**
 * findMid
 *
 * @author alcohol
 * @Description
 * @since 2024-09-17 12:38
 */
public class findMid {

    static Queue<Integer> min;
    static Queue<Integer> max;

    public findMid(){
        min = new PriorityQueue<>((x,y) -> (y-x));
        max = new PriorityQueue<>();
    }

    public static void add(int num){
        if(min.size() == max.size()){
            min.add(num);
            max.add(min.poll());
        }else{
            max.add(num);
            min.add(max.poll());
        }
    }

    public static double getMid(){
        if(min.size() == max.size()){
            return (min.peek()+max.peek())*0.5;
        }else{
            return max.peek()*1.0;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine(); // 消耗换行符
        findMid fm = new findMid();

        for(int i = 0 ; i < n ; i ++){
            String ope = sc.nextLine();
            if(ope.equals("addNum")){
                System.out.println("输入添加的数");
                int num = sc.nextInt();
                sc.nextLine(); // 消耗换行符
                fm.add(num);
            }else if(ope.equals("findMedian")){
                System.out.println(fm.getMid());
            }
        }
    }
}


http://www.kler.cn/news/315511.html

相关文章:

  • 五、CAN总线
  • C++:动态内存分配(new、delete 相比 malloc、free的优势)与运算符重载
  • 线程池动态设置线程大小踩坑
  • Hadoop的安装和使用
  • 【JavaScript】数据结构之树
  • Qt 学习第十天:小项目:QListWidget的使用
  • 【基于Spring Boot的汽车租赁系统】
  • 【微信小程序】连续拍照功能实现
  • kafka 消息位移提交几种方式:消息重复消息、消息丢失的关键
  • Docker_基础初识
  • 新能源汽车知识点集萃
  • Python办公自动化教程(003):PDF的加密
  • HarmonyOS Next开发----使用XComponent自定义绘制
  • 【乐企-工具篇】有关乐企发票文件生成- OFD和PDF文件生成
  • 四、JVM原理-4.1、JVM介绍
  • vue中 <template> 与 <template lang=“jade“>的对比,哪个性能好
  • 数据结构之希尔排序
  • 轻代码的概念学习笔记
  • http和https的区别及get和post请求的区别
  • Vue3新组件transition(动画过渡)
  • Java API 之集合框架进阶
  • 软件测试面试题(5)——二面(游戏测试)
  • 【PLW003】设备器材云端管理平台v1.0(SpringBoot+Mybatis+NodeJS+MySQL前后端分离)
  • LeetCode题练习与总结:回文链表--234
  • [JavaEE]———进程、进程的数据结构、进程的调度
  • 【优选算法之二分查找】No.5--- 经典二分查找算法
  • Linux之实战命令03:stat应用实例(三十七)
  • 如何使用 maxwell 同步到 redis?
  • 如何在 CentOS 中管理用户、组和服务状态
  • git pull的merge和rebase模式