Implement the following operations of a stack using queues. (Easy)

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.

Notes:

  • You must use only standard operations of a queue -- which means only push to backpeek/pop from frontsize, and is empty operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

分析:

使用两个队列实现,其中一个队列为空,用来进行颠倒顺序和输出队尾元素。

入栈操作: 向非空队列内入队即可

出栈操作:将非空队列除队尾元素外的所有元素导入另一个空队列,剩余队尾元素即为待应该待出栈元素

top()操作: 同出栈,注意只需访问返回,不需要让其出队,即仍需将其导入另一队列

注意:两队列地位平等,均可能用作储存和转移工作

代码:

 class Stack {
private:
queue<int> q1,q2;
public:
// Push element x onto stack.
void push(int x) {
if(!q1.empty()){
q1.push(x);
}
else{
q2.push(x);
}
} // Removes the element on top of the stack.
void pop() {
if(!q1.empty()){
while(q1.size() > ){
q2.push(q1.front());
q1.pop();
}
q1.pop();
}else{
while(q2.size() > ){
q1.push(q2.front());
q2.pop();
}
q2.pop();
}
} // Get the top element.
int top() {
if(!q1.empty()){
while(q1.size() > ){
q2.push(q1.front());
q1.pop();
}
int ans = q1.front();
q2.push(q1.front());
q1.pop();
return ans;
}else{
while(q2.size() > ){
q1.push(q2.front());
q2.pop();
}
int ans = q2.front();
q1.push(q2.front());
q2.pop();
return ans;
}
} // Return whether the stack is empty.
bool empty() {
if(q1.empty() && q2.empty()){
return true;
}
return false;
}
};
 

LeetCode225 Implement Stack using Queues的更多相关文章

  1. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  2. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  3. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  4. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  5. Implement Queue by Two Stacks & Implement Stack using Queues

    Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...

  6. [Swift]LeetCode225. 用队列实现栈 | Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  7. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  8. Java for LeetCode 225 Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  9. Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

随机推荐

  1. CentOS 6.5 usb安装

    我只说一下将CentOS安装作为服务器的情况,但是适用于普遍的CentOS安装过程,首先下载CentOS的安装镜像文件,有这么几种:CentOS-6.5-x86_64-LiveCD.CentOS-6. ...

  2. IO流 输入和输出文档内容

    package io; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io. ...

  3. PetaPoco 基础操作

    //初始化数据库连接 var db=new PetaPoco.Database("connectionStringName"); //查询单个值 long count=db.Exe ...

  4. c++使用优先队列时自定义优先出队顺序(和sort)

    优先队列也是一种先进先出的数据结构,元素从队尾入队,从队头出队,但是优先队列相较一般队列多了一个判断优先级的功能,在当前队列中,优先级最高的元素将被第一个删除. 先看一下优先队列的定义 templat ...

  5. 关于在静态html中实现语言切换的思路与实现

    在项目中只用到了三种语言:英文.中文简体.中文繁体.所以我首先想到了最笨的方法:1.直接将三种语言写在html中,显示当前设置的语言隐藏其它两种来实现.2.使用css伪元素的content:attr( ...

  6. windows 和 mac 文件夹共享问题汇总

    目标:windows上的文件夹,共享给MAC,mac可以将文件复制到windows上来 windows设置共享文件夹,然后在mac上访问 假设win的ip地址是10.10.27.11,则mac上远程方 ...

  7. clientHeight、offsetHeight 区别 笔记

    一张图 说明全部 clientHeight和clientWidth用于描述元素内尺寸,是指元素内容+内边距大小,不包括边框(低版本IE下实际包括).外边距.滚动条部分 offsetHeight和off ...

  8. Windows下Git的下载、安装、设置用户名和邮箱、创建版本库等

    Git官网:https://git-scm.com/ 一.Git下载 官网首页下载,当前最新版本:2.24.1 本人下载的是Git for Windows版本:Git-2.24.1.2-64-bit. ...

  9. 面试Nginx的几个常见问题(

    1.Nginx 服务器上的 Master 和 Worker 进程分别是什么 Master 进程:读取及评估配置和维持 Worker 进程:处理请求   2.怎么添加模块? Firstly, you h ...

  10. jframe 设置左上角和任务栏的图标

    默认就是 改成有意义的,一眼就能看出来功能的,比如一个小蜘蛛 第一个最简单的做法,把图片扔到工程的根目录,但是这样会相当乱,不便于文件管理 ImageIcon icon = new ImageIcon ...