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

DAY 47 leetcode 232--栈与队列.用栈实现队列

题号232

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

class MyQueue {Stack<Integer> stackIn;Stack<Integer> stackOut;/** Initialize your data structure here. */public MyQueue() {stackIn = new Stack<>(); // 负责进栈stackOut = new Stack<>(); // 负责出栈}/** Push element x to the back of queue. */public void push(int x) {stackIn.push(x);}/** Removes the element from in front of queue and returns that element. */public int pop() {    dumpstackIn();return stackOut.pop();}/** Get the front element. */public int peek() {dumpstackIn();return stackOut.peek();}/** Returns whether the queue is empty. */public boolean empty() {return stackIn.isEmpty() && stackOut.isEmpty();}// 如果stackOut为空,那么将stackIn中的元素全部放到stackOut中private void dumpstackIn(){if (!stackOut.isEmpty()) return; while (!stackIn.isEmpty()){stackOut.push(stackIn.pop());}}
}

http://www.dtcms.com/a/139144.html

相关文章:

  • 视频剪辑入门
  • iptables防火墙
  • 【C++初阶】第14课—缝合怪deque和优先队列、仿函数
  • AI在能源消耗管理及能源效率提升中的核心应用场景及技术实现
  • vllm 本地部署qwen2.5/Qwen2.5-32B-Instruct-AWQ模型
  • Crawl4AI:为大语言模型打造的开源网页数据采集工具
  • AI Agent系列(九) -Data Agent(数据分析智能体)
  • 【无人机】PX4飞控怎么烧写加载固件,更新固件方法,详细流程
  • seaborn库详解
  • pandas库详解
  • 【web服务_负载均衡Nginx】一、Nginx 基础与核心概念解析
  • 使用 reverse-sourcemap 工具反编译 Vue 项目
  • 如何通俗理解transformer架构
  • Vue中v-if和v-show区别
  • 探索Web3平台的数据安全和保护机制
  • 蓝光三维扫描技术:高效精密测量相机镜头底座注塑件
  • 在 Power BI Desktop 中设置视觉对象查询限制
  • 基于labview的钢琴程序设计
  • 算法之贪心算法
  • Python + 链上数据可视化:让区块链数据“看得懂、用得上”
  • ESP32- 开发笔记- 硬件设计-ESP32-C3基本电路
  • Windows平台使用Docker部署Neo4j
  • 《MySQL:MySQL数据库的基本操作》
  • 利用互斥锁或者利用逻辑过期解决缓存击穿问题
  • C# 类型、存储和变量(实例化类型)
  • Flutter的原理及美团的实践(下)
  • Java 中 Synchronized如何保证可见性
  • 访问者模式深度解析与实战案例
  • AI日报 - 2025年4月18日
  • 【KWDB 创作者计划】深度实操体验 KWDB 2.2.0:从安装到实战的全流程解析以及实操体验