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

队列相关练习

目录

1、用队列实现栈

2、用栈实现队列


1、用队列实现栈

oj:225. 用队列实现栈 - 力扣(LeetCode)

思路:

1. 入栈时,两个队列 哪个不为空放到哪个队列里,两个都是空的指定放到第一个里面

2. 出栈时,哪个不为空出哪个,出 size-1 个元素

3. 当两个队列都是空的时候,那么说明我们模拟的栈是空的

class MyStack {

    private Queue<Integer> qu1;
    private Queue<Integer> qu2;

    public MyStack() {
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();
    }
    
    public void push(int x) {
        if(!qu1.isEmpty()) {
            qu1.offer(x);
        }else if(!qu2.isEmpty()) {
            qu2.offer(x);
        }else {
            //两个队列都是空的 指定存放到第一个队列当中
            qu1.offer(x);
        }
    }
    
    public int pop() {
        if(empty()) {
            return -1;
        }
        if(!qu1.isEmpty()) {
            int size = qu1.size();
            for (int i = 0; i < size-1; i++) {
                int x = qu1.poll();
                qu2.offer(x);
            }
            return qu1.poll();
        }else{
            int size = qu2.size();
            for (int i = 0; i < size-1; i++) {
                int x = qu2.poll();
                qu1.offer(x);
            }
            return qu2.poll();
        }

    }
    
    public int top() {
        if(empty()) {
            return -1;
        }
        if(!qu1.isEmpty()) {
            int size = qu1.size();
            int x = -1;
            for (int i = 0; i < size; i++) {
                 x = qu1.poll();
                qu2.offer(x);
            }
            return x;
        }else{
            int size = qu2.size();
            int x = -1;
            for (int i = 0; i < size; i++) {
                x = qu2.poll();
                qu1.offer(x);
            }
            return x;
        }
    }
    
    public boolean empty() {
        //两个队列同时为空的时候 说明模拟实现的栈是空的!
        return qu1.isEmpty() && qu2.isEmpty();
    }
}

2、用栈实现队列

oj:232. 用栈实现队列 - 力扣(LeetCode)

思路:

1. 入队时,放到第一个栈里面

2. 出队时,只出第2个栈中的元素

3. 当第2个栈没有元素的时候,把第一个栈当中的元素全部倒在第二个栈中

class MyQueue {

    private Stack<Integer> s1;
    private Stack<Integer> s2;

    public MyQueue() {
        s1 = new Stack<>();
        s2 = new Stack<>();
    }
    
    public void push(int x) {
        s1.push(x);
    }
    
    public int pop() {
        if(empty()) {
            return -1;
        }
        if(s2.empty()) {
            while (!s1.empty()) {
                s2.push(s1.pop());
            }
        }
        //
        return s2.pop();
    }
    
    public int peek() {
        if(empty()) {
            return -1;
        }
        if(s2.empty()) {
            while (!s1.empty()) {
                s2.push(s1.pop());
            }
        }
        return s2.peek();
    }
    
    public boolean empty() {
        return s1.empty() && s2.empty();
    }
}

http://www.kler.cn/a/576790.html

相关文章:

  • springboot多模块项目中IDEA报错找不到符号
  • 基于51单片机的六路抢答器proteus仿真
  • DeepSeek的安全威胁与国际象棋水平测试
  • 云计算:虚拟化、容器化与云存储技术详解
  • FPGA之USB通信实战:基于FX2芯片的Slave FIFO回环测试详解
  • 【CVPR2025】 EVSSM:用状态空间模型高效去模糊
  • 基于粒子群算法的配电网重构
  • 中性点不接地系统单相接地故障Matlab仿真
  • python 使用flask+sqlalchemy 实现简单数据查询接口
  • 【江协科技STM32】ADC数模转换器-学习笔记
  • MWC2025|5G与AI的深度融合势不可挡,赛思高精度时钟同步为其筑基!
  • 如何在Ubuntu上直接编译Apache Doris
  • 小方摄像头接入本地服务器的方法
  • 深入剖析MyBatis缓存机制:原理、源码与实战指南
  • 深度评测阿里云操作系统控制台:功能全面,体验卓越!
  • Debian系统grub新增启动项
  • 使用OpenCV来获取视频的帧率
  • element-plus表格操作列点击事件会触发行点击事件
  • Array and string offset access syntax with curly braces is deprecated
  • HTML-网页介绍