LeetCode 232: Implement Queue using Stacks
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, andoperations are valid.
is empty - 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).
题目要求通过栈来模拟队列的行为。与此类似的还有通过队列模拟栈(http://blog.csdn.net/sunao2002002/article/details/46482673),此题是算法导论第十章的一道题。算法例如以下:
堆栈a和b。a用作入队,b出队
(1)判队满:假设a满且b不为空,则队满
(2)判队空:假设a和b都为空,则队空
(3)入队:首先判队满。
若队不满:(1)栈a若不满,则直接压入栈a
(2)若a满,则将a中的全部元素弹出到栈b中,然后再将元素入栈a
(4)出队:(1)若b空就将a中的全部元素弹出到栈b中。然后出栈
(2)b不空就直接从b中弹出元素
代码例如以下:
class Queue {
public:
// Push element x to the back of queue.
stack<int> in;
stack<int> out;
void push(int x) {
in.push(x);
}
void move(){
while(!in.empty())
{
int x = in.top();
in.pop();
out.push(x);
}
}
// Removes the element from in front of queue.
void pop(void) {
if (out.empty())
{
move();
}
if (!out.empty())
{
out.pop();
}
}
// Get the front element.
int peek(void) {
if (out.empty())
{
move();
}
if (!out.empty())
{
return out.top();
}
}
// Return whether the queue is empty.
bool empty(void) {
return in.empty() && out.empty();
}
};
LeetCode 232: Implement Queue using Stacks的更多相关文章
- LeetCode 232:Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back ...
- LeetCode OJ:Implement Queue using Stacks(栈实现队列)
比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如 ...
- 【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( ...
- 【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...
- LeetCode(232) Implement Queue using Stacks
题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...
- LeetCode算法题-Implement Queue Using Stacks(Java实现)
这是悦乐书的第195次更新,第201篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第57题(顺位题号是232).使用栈实现队列的以下操作. push(x) - 将元素x推 ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
随机推荐
- S3C6410 LCD Overlay Test Program
测试了一下S3C6410 LCD控制器提供的Overlay功能,写了一个简单的test,主要用于实现FB0和FB1之间的Overlay操作.内核选项的Windows至少要为2. 具体支持如下操作:1 ...
- python--(爬虫-re模块)
python--(爬虫-re模块) re模块四大核心功能: 1.findall 查找所有,返回list import re lst = re.findall("m", " ...
- Vue 做项目经验
Vue 做项目经验 首先需要知道最基本的东西是: Vue 项目打包:npm run build Vue生成在网页上看的端口:npm run dev 修改端口号的地方在: config文件夹下index ...
- SpringAOP之CGLIB字节码增强
SpringAOP的基础原理就是动态代理 有两种实现方式:1)jdk动态代理 2)cglib动态代理 jdk动态代理和cglib动态代理的区别在于: cglib没有接口(通过继承父类) 只有实现类. ...
- 新建Eclipse工作空间,复制原有的配置(转)
方法一: File->Switch workspace->Other...,按下图选择 只复制简单的配置,如cvs之类的信息是不会复制的. 方法二: 在方法一的基础上做如下操作 将新建的w ...
- SQLSERVER-存储过程知识点
原文链接:http://www.qeefee.com/article/000566 存储过程是一组预编译的SQL语句,它可以包含数据操纵语句.变量.逻辑控制语句等. 存储过程允许带参数: 输入参数:可 ...
- [ES2018] Two ways to write for-await-of
// Asynchronous iteration --> Symbol.asyncIterator async function main() { const syncIterable = [ ...
- Genymotion出现Installation error: INSTALL_FAILED_CPU_ABI_INCOMPATIBLE错误解决方法
今天在Genymotion上执行曾经的一个项目(libs中有多个SDK和so文件)时,出现下面错误: Console控制台中:Installation error: INSTALL_FAILED_CP ...
- mysql的查询练习1
1.多表查询
- js算法:分治法-棋盘覆盖
在一个 2^k * 2^k 个方格组成的棋盘中,若恰有一个方格与其他方格不同.则称该方格为一特殊方格,称该棋盘为一特殊棋盘.显然特殊方格在棋盘上出现的位置有 4^k 种情形.因而对不论什么 k> ...