题目:
Implement the following operations of a queue using stacks.

push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.
Notes:
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

思路:
用两个栈实现队列,s1枯是队尾,s1的底是队头。在pop时,要借助s2,将s1的数都放到s2上,然后将s2的顶(即s1的底)pop; 取peek时,即将pop时的动作改为top即可;在push时,首先要将s2中的数再还原到s1,然后push;判断是否为empty要s1和s2都为empty时,队列才empty。

代码:

class MyQueue {public:/** Initialize your data structure here. */MyQueue() {}/** Push element x to the back of queue. */void push(int x) {while (!s2.empty()){//如果s2不为空s1.push(s2.top());//那就就先要将s2中的数据全都放到s1中s2.pop();}s1.push(x);//入队列}/** Removes the element from in front of queue and returns that element. */int pop() {while (!s1.empty()){//如果s1不为空s2.push(s1.top());//要将s1中的数据放到s2中,再取s2的栈顶元素s2.top()s1.pop();//再pop}int result = s2.top();//出队列s2.pop();return result;}/** Get the front element. */int peek() {while (!s1.empty()){//如果s1不为空s2.push(s1.top());//要将s1中的数据放到s2中,再取s2的栈顶元素s2.top()s1.pop();//再pop}return s2.top();}/** Returns whether the queue is empty. */bool empty() {return s1.empty()&&s2.empty();//两个栈都为空时,队列才空}
private:stack<int> s1;stack<int> s2;
};/*** Your MyQueue object will be instantiated and called as such:* MyQueue obj = new MyQueue();* obj.push(x);* int param_2 = obj.pop();* int param_3 = obj.peek();* bool param_4 = obj.empty();*/

LeetCode 232. Implement Queue using Stacks相关推荐

  1. LeetCode 232. Implement Queue using Stacks--用2个栈来实现一个队列--C++解法

    LeetCode 232. Implement Queue using Stacks–C++解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大 ...

  2. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  3. leetcode python3 简单题232. Implement Queue using Stacks

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十二题 (1)题目 英文: Implement the following ...

  4. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

  5. C#LeetCode刷题之#232-用栈实现队列​​​​​​​​​​​​​​(Implement Queue using Stacks)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4108 访问. 使用栈实现队列的下列操作: push(x) -- ...

  6. 使用栈实现队列 Implement Queue using Stacks

    为什么80%的码农都做不了架构师?>>>    问题: Implement the following operations of a queue using stacks. pus ...

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

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

  8. LeetCode Implement Queue using Stacks (数据结构)

    题意: 用栈来实现队列. 思路: 一个栈是不够的,至少要两个. (1)插入.永远只插入到stack1中(插到栈顶). (2)弹出.如果stack2不为空,直接弹出stack2的栈顶,否则,将stack ...

  9. [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

    3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...

最新文章

  1. HTML5 canvas 初级入门教程【转】
  2. struts2中拦截器和过滤器的比较
  3. java 常量变量使用_java常量和变量入门教程
  4. 【转】小白级的CocoaPods安装和使用教程
  5. 在PyTorch中转换数据
  6. mysql update 跳过重复_MySQL通过UPDATE / DELETE整合重复的数据记录
  7. [Asp.net]Uploadify上传大文件,Http error 404 解决方案 - wolfy
  8. 如何处理不能复现的bug?软件测试工程师避坑指南
  9. tomcat配置SSL加密网站
  10. 调度失败:执行器地址为空_三千字带你搞懂XXL-JOB任务调度平台
  11. Jnotify文件监控的用法以及Jar文件导入的方法
  12. 数字图像处理笔记——Snakes算法、活动轮廓以及水平集(Snakes, active contours, and level sets)
  13. 用word这么多年,90%的人居然不知道这12个小技巧
  14. ecmobile php开发文档,ecmobile PHP接口说明文档之购物车(cart/create|list|detele|update)
  15. 硬盘三大种类( SSD ; HHD ; HDD )
  16. 数据库设计、查询规范及常用SQL语句
  17. mysql explain不准确_mysql explain预估剖析
  18. opengl实现太阳系、地球系,并加上地球的贴图
  19. 编写iptables模块实现不连续IP地址的DNAT-POOL
  20. 如何调试分布式系统:与微服务调试工具“Squash”创始人Idit Levine的对话

热门文章

  1. linux c语言 udp 接收和发送数据用同一个端口_网络编程基础入门及TCP,UDP
  2. python监控桌面捕捉_想要远程获取对方的桌面?Python教你一招搞定!
  3. C++:构造函数的重载
  4. (SRNTT)Image Super-Resolution by Neural Texture Transfer
  5. 操作系统(李治军) L12内核级线程的实现
  6. 计算机网络(2)-数据链路层流量控制
  7. Ubuntu 上寻找依赖库(.so)的方法
  8. v-bind:href= 拼接字符串 - 代码篇
  9. layui入门基础:资源汇总
  10. python计算分段函数_Python练习题2.2计算分段函数