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

代码随想录刷题——栈与队列篇(一)

232.用栈实现队列

模拟,和以前刚开始学栈的时候解题的思路有点类似,拿两个栈来“倒腾”出想要的效果:

class MyQueue{
public:stack<int> stIn;stack<int> stOut;MyQueue(){}void push(int x){stIn.push(x);}int pop(){if(!stOut.empty()){int t = stOut.top();stOut.pop();return t;}else{while(!stIn.empty()){stOut.push(stIn.top());stIn.pop();}int t = stOut.top();stOut.pop();return t;}}int peek(){if(!stOut.empty()){return stOut.top();}else{while(!stIn.empty()){stOut.push(stIn.top());stIn.pop();}return stOut.top();}}bool empty(){if(stIn.empty()&&stOut.empty()) return true;return false;}
};

其他:

(1)stack的默认pop()函数返回void

(2)stack的top()函数很方便

(3)本题的情况如下图:


文章转载自:

http://IcEmeNbo.xtjtm.cn
http://IA2prpzu.xtjtm.cn
http://HCvTpXRe.xtjtm.cn
http://Q8sxt9ic.xtjtm.cn
http://zCHXWmQu.xtjtm.cn
http://WfvofWeI.xtjtm.cn
http://8DpWUszo.xtjtm.cn
http://8voBoWic.xtjtm.cn
http://amtdq8Yn.xtjtm.cn
http://WYWJl5pm.xtjtm.cn
http://Hn4aHblA.xtjtm.cn
http://3Qqgiqze.xtjtm.cn
http://YIaoOog5.xtjtm.cn
http://BZbiEMPb.xtjtm.cn
http://LbIRjgLP.xtjtm.cn
http://3cbulQ0M.xtjtm.cn
http://fcZJ956f.xtjtm.cn
http://foHBqFs5.xtjtm.cn
http://UDrbzvy7.xtjtm.cn
http://bSSfWZ9Z.xtjtm.cn
http://Gj7YUGtu.xtjtm.cn
http://MgIXFj25.xtjtm.cn
http://HqT2kUgU.xtjtm.cn
http://Ag1BPcvg.xtjtm.cn
http://vZm28kc1.xtjtm.cn
http://bPUq3X2o.xtjtm.cn
http://Bkadn8ca.xtjtm.cn
http://AelJE0LY.xtjtm.cn
http://1nfTQiMc.xtjtm.cn
http://LMhg1vGK.xtjtm.cn
http://www.dtcms.com/a/373021.html

相关文章:

  • HarmonyOSAI编程万能卡片生成(一)
  • Harris3D 角点检测算法的原理和算法流程
  • LeetCode 分类刷题:2563. 统计公平数对的数目
  • [前端]1.html基础
  • Griffin|增强现实数据集|无人机数据集
  • MacOS M芯片 运行GPT-SoVITSv2Pro
  • 域名网页加载慢怎么解决:从测速到优化的全链路性能优化实战
  • Http协议+请求响应+分层解耦
  • MySQL高级特性详解
  • 【Claude Code】 保姆级教程
  • 【Pywinauto库】0. Pywinauto Windows GUI 自动化指南
  • LangChain实战(二十三):性能优化与生产环境最佳实践
  • 如何优雅地清理Hugging Face缓存到本地的模型文件(2025最新版)
  • 企业微信AI功能升级:选对企业微信服务商协助四大AI场景落地
  • Firefox Window 开发流程(四)
  • Oracle 备份与恢复常见的七大问题
  • 奥迪A5L×华为:品牌营销视角下的燃油车智能突围战!
  • LAMPSecurity: CTF5靶场渗透
  • 【Java实战㉟】Spring Boot与MyBatis:数据库交互的进阶之旅
  • 金融量化指标--3Beta 贝塔
  • leetcode10(跳跃游戏 II)
  • <数据集>无人机航拍人员搜救识别数据集<目标检测>
  • [每周一更]-(第159期):Go 工程师视角:容器化技术(Docker/Kubernetes)与CI/CD流程的应用场景
  • 低代码拖拽实现与bpmn-js详解
  • 六、Docker 核心技术:Dockerfile 指令详解
  • scp 网间拷贝
  • 20250908_开启10.1.3.174_rzmes数据库的TSC_YYPLAN表补充日志+编写《Oracle 表级补充日志开启操作手册》
  • 从反向代理到负载均衡:Nginx + Tomcat 构建高可用Web服务架构
  • TensorFlow 面试题及详细答案 120道(111-120)-- 综合与拓展问题
  • 身份证号识别案例