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, 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).
分析
用栈实现队列;
只需要两个辅助栈,一个保存压入元素,一个保存弹出元素;当弹出元素栈空时,把所有入栈元素压入出栈;
AC代码
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
inStack.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if (outStack.empty())
{
while (!inStack.empty())
{
outStack.push(inStack.top());
inStack.pop();
}
}
outStack.pop();
}
// Get the front element.
int peek(void) {
if (outStack.empty())
{
while (!inStack.empty())
{
outStack.push(inStack.top());
inStack.pop();
}
}
return outStack.top();
}
// Return whether the queue is empty.
bool empty(void) {
if (outStack.empty() && inStack.empty())
return true;
return false;
}
private:
stack<int> inStack;
stack<int> outStack;
};
LeetCode(232) Implement Queue using Stacks的更多相关文章
- 【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 232: Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- LeetCode(28)Implement strStr()
题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if nee ...
- LeetCode(225) Implement Stack using Queues
题目 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- 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 ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
随机推荐
- 爬虫(正则)——爬neihan8
工具:python3 步骤: 1)定义spider类,将具体功能封装成方法 2)观察html页面,写好正确的正则表达式来找到符合规则的文字 3)写一个方法测试一下,保证每写好一个都是对的 4)加入提示 ...
- AD7606笔记
V1~V8共8个ADC通道: REFIN/OUT:基准电源,可选择内部(REF_SLECT=1)的或者外部的(REF_SLECT=0) VDIRVE:MCU的的VCC,2.3~5V.逻辑电平指的是需要 ...
- P2737 [USACO4.1]麦香牛块Beef McNuggets 数学题 + 放缩思想
https://www.luogu.org/problem/show?pid=2737#sub 先说一个结论:对于两个数p, q,且gcd(p, q) = 1(这个很重要,是条件来的).他们不能组合成 ...
- (转)io各层次性能汇总及运行速度对比
io各层次性能汇总:以上图片可以清晰的解释io的运行效率 守护进程:持续保持运行着的程序 进程:放在内存中运行的程序 程序:代码文件,php,java
- 关于C#操作Excel,复制Sheet的记录
1.先用了NPOI,去做,HSSFWorkbook 里面有一个Copy方法,但这个只支持office2003. 对应的XSSFWorkbook没有些方法. 而且这个这个方法对devexpress导出的 ...
- 仙人掌(cactus)
题目描述LYK 在冲刺清华集训(THUSC)!于是它开始研究仙人掌,它想来和你一起分享它最近研究的结果.如果在一个无向连通图中任意一条边至多属于一个简单环(简单环的定义为每个点至多经过一次),且不存 ...
- tyvj P4877 _1.组合数
时间限制:1s 内存限制:256MB [问题描述] 从m个不同元素中,任取n(n≤m)个元素并成一组,叫做从m个不同元素中取出n个元素的一个组合:从m个不同元素中取出n(n≤m)个元素的所有组合的个数 ...
- java读取文件封装的一个类(有部分代码借鉴别人的)
package modbus.rtu.calc; import java.io.BufferedReader; import java.io.FileInputStream; import java. ...
- Myeclipse发布第一个jsp页面及web project部署到tomcat上的几种方法
菜鸟日记: 1:new web project: 2:fix the visiting path of the tomcat,打开在安装目录下conf目录中的server.xml,在</Hos ...
- android 跨进程通讯 AIDL
跨进程如何通讯?两个进程无法直接通讯,通过Android系统底层间接通讯.基于service的aidl实现跨进程通讯. 什么叫AIDL? Android interface definition la ...