用两个栈实现,方法有2
1.一个是辅助栈
2.一个栈只入栈,一个栈只出栈。


  • 1
class MyQueue {public:/** Initialize your data structure here. */stack<int>st1;stack<int>st2;int topp;MyQueue() {}/** Push element x to the back of queue. */void push(int x) {if(st1.empty()) topp=x;st1.push(x);}/** Removes the element from in front of queue and returns that element. */int pop() {int ans;int size=st1.size()-1;stack<int>().swap(st2);while(size--){st2.push(st1.top());st1.pop();}if(!st2.empty())topp=st2.top();ans=st1.top();stack<int>().swap(st1);size=st2.size();while(size--){st1.push(st2.top());st2.pop();}return ans;}/** Get the front element. */int peek() {return topp;}/** Returns whether the queue is empty. */bool empty() {return st1.empty();}
};
  • 2
class MyQueue {public:/** -Initialize your data structure here. */stack<int>st_push;stack<int>st_pop;MyQueue() {}/** Push element x to the back of queue. */void push(int x) {st_push.push(x);}/** Removes the element from in front of queue and returns that element. */int pop() {if(st_pop.empty()){while(!st_push.empty()){st_pop.push(st_push.top());st_push.pop();}}int ans=st_pop.top();st_pop.pop();return ans;}/** Get the front element. */int peek() {if(st_pop.empty()){while(!st_push.empty()){st_pop.push(st_push.top());st_push.pop();}}return st_pop.top();}/** Returns whether the queue is empty. */bool empty() {return st_pop.empty()&&st_push.empty();}
};

232.用栈实现队列相关推荐

  1. 【Leetcode】232.用栈实现队列

    题目链接:232.用栈实现队列 关键字:栈.队列 解题思路 使用两个栈实现队列,其中一个栈正常使用,另一个栈用来保存逆序数据 代码实现 class MyQueue { public:/** Initi ...

  2. Suzy找到实习了吗Day 10 | 栈和队列开始啦:232. 用栈实现队列,225. 用队列实现栈

    day10 python栈的实现 Python栈所需要的包 232. 用栈实现队列 思路 solution ?? 225. 用队列实现栈 思路 solution python栈的实现 Python中现 ...

  3. 【代码随想录刷题记录】 232.用栈实现队列 、225. 用队列实现栈

    232.用栈实现队列 题目 请你仅使用两个栈实现先入先出队列.队列应当支持一般队列支持的所有操作(push.pop.peek.empty): 实现 MyQueue 类: void push(int x ...

  4. 代码随想录【day 10 栈与队列】| 232.用栈实现队列、 225. 用队列实现栈

    代码随想录[day 10 栈与队列]| 232.用栈实现队列. 225. 用队列实现栈 理论基础 LeetCode 232.用栈实现队列 题目链接:232.用栈实现队列 卡哥文解 视频讲解 解题思路( ...

  5. 代码随想录第十天 | 225. 用队列实现栈 232.用栈实现队列(Java))

    232.用栈实现队列 class MyQueue {Stack<Integer> stackIn;Stack<Integer> stackOut;public MyQueue( ...

  6. 【栈】【232. 用栈实现队列】【简单】

    使用栈实现队列的下列操作: push(x) – 将一个元素放入队列的尾部. pop() – 从队列首部移除元素. peek() – 返回队列首部的元素. empty() – 返回队列是否为空. 示例: ...

  7. LeetCode 232. 用栈实现队列(双栈法-队列)

    1. 题目 使用栈实现队列的下列操作: push(x) – 将一个元素放入队列的尾部. pop() – 从队列首部移除元素. peek() – 返回队列首部的元素. empty() – 返回队列是否为 ...

  8. leetcode 232. 用栈实现队列(Implement Queue using Stacks)

    目录 题目描述: 示例: 说明: 解法: 题目描述: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列 ...

  9. 【Java】LeetCode 232. 用栈实现队列

    题目描述 : 请你仅使用两个栈实现先入先出队列.队列应当支持一般队列支持的所有操作(push.pop.peek.empty): 实现 MyQueue 类: void push(int x) 将元素 x ...

  10. Leetcode 232. 用栈实现队列 解题思路及C++实现

    解题思路: 使用两个栈,一个栈a用来存储每一次操作得到的队列,另一个栈b作为辅助栈. 在每一次push操作的时候,先把a中排好的队列(先进先出),依次push进栈b,所以,栈b中元素的排序就是后进先出 ...

最新文章

  1. oracle1core,Oracle core06_latchlock
  2. mysql isolation level_MySQL数据库事务隔离级别(Transaction Isolation Level)
  3. 【数据平台】关于Hadoop集群namenode format安全事故
  4. linux samba代码,Linux下Samba服务器源码安装及配置
  5. 响应式网页设计代码_消除响应式网站建设设计中的缺陷
  6. 头条号为什么把作者抛弃了?
  7. 在线学习新编程 技巧全攻略
  8. Sass--伪类嵌套
  9. hive获取本周第几天
  10. 安全生产应急救援信息管理指挥调度系统
  11. 解放生产力「GitHub 热点速览 v.21.51」
  12. PHP实现讯飞语音转写demo
  13. 出色的社区网站_《最后的我们》中出色的制作系统
  14. 手机cpu天梯图2020
  15. openlayers6【五】地图图层数据来源 source 详解
  16. 免安装版MySQL(解压版)安装详细教程及注意事项
  17. 原来她在我的生命中如此重要
  18. 华为BGP协议基础配置
  19. LeetCode1109之航班预订统计(相关话题:差分数组)
  20. android实习报告,基于Android的毕业实习报告.doc

热门文章

  1. Installing Oracle Database 18c Using RPM Packages
  2. python3 django配置数据库(mysql)
  3. python爬虫之cookie方式自动登录巴比特网
  4. 《VMware Virtual SAN权威指南》一2.2 VSAN的要求
  5. JSP HTML区别
  6. Command模式的实践
  7. java 基础算法教程ppt,基础排序算法(附加java实现)
  8. 服务器收到消息怎么推送给app_「刹那问答24」浅谈FCM推送
  9. php 爬虫去重,浅谈动态爬虫与去重(续)
  10. 机器人x展架制作_易拉宝展架设计制作常规尺寸材质有哪些?