LeetCode225 Implement Stack using Queues
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 back,peek/pop from front,size, andis emptyoperations 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的更多相关文章
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【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( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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 ...
- 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) -- ...
- [Swift]LeetCode225. 用队列实现栈 | Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 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 ...
- Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- PAT甲级——A1065 A+B and C (64bit)
Given three integers A, B and C in [−], you are supposed to tell whether A+B>C. Input Specificati ...
- Python configparser的使用 读取配置文件
configparser是Python自带用于读取配置文件的工具包,它的使用非常简单 配置文件 配置文件[]中为section命名,section的命名可以包含空格,每个section下面以键值对的方 ...
- http协议与servletl理解
协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器 s ...
- angular7.X配置同时使用localhost和本机IP访问项目
1.项目中找到此文件“node_modules/webpack-dev-server/lib/Server.js”,按照下图修改: 二.修改配置文件package.json,见下图: 三.npm st ...
- 大量的Close_wait 发现的 too many open file 错
突然频繁出现大量的 Close_wait,查看程序日志,发现 connection 在 accept 时报错,Too many open file. 估计程序里有这个漏洞,当 accept 时报错,没 ...
- php解决高并发(文件锁)
文件锁分为两种方式: [一].阻塞模式:(如果其他进程已经加锁文件,当前进程会一直等其他进程解锁文件后继续执行) <?php //连接数据库 $con=mysqli_connect(" ...
- Django 创建web项目之HelloWorld
Django.Flask.Tornado并称为python WEB三大框架.Diango是一个开源的web应用框架,具有开发速度快的特点.同时因为过度封装,具有性能低的特点. 创建Django项目,启 ...
- Ubuntu 服务器默认的root账号是没有激活的,需要用初装的用户账号给root设置管理密码
user@ubuntu12:~$ sudo password root //用sudo修改账户 1.根据提示输入当前用户的密码: 2.修改成功之后你就可以使用root账号了,可以使用su root 命 ...
- python 常见分布的产生方式
- 强强联合 阿里云 RDS for SQL Server 与 金蝶 K/3 WISE 产品实现兼容适配
强强联合 阿里云 RDS for SQL Server 与 金蝶 K/3 WISE 产品实现兼容适配,原K/3 WISE用户通过简单配置就可以无缝搭配RDS SQL Server使用,不需再费时费力自 ...