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(). 移除队首元 ...
随机推荐
- net core 2.0 web api + Identity Server 4 + angular 5
net core 2.0 web api + Identity Server 4 + angular 5前台使用angular 5, 后台是asp.net core 2.0 web api + ide ...
- Hart协议
官方https://fieldcommgroup.org/technologies/hart/documents-and-downloads-hart 参考网页http://www.eeworld.c ...
- asp.net 多语言 在IIS7.5发布出现找不到资源文件
我也遇到这个问题,纠结了半天, 最后把资源文件的属性改为:内容 就可以了. 见:http://q.cnblogs.com/q/60443/
- mysql导入sql文件错误#1044 - Access denied for user 'root'@'localhost'
在我的个人知识管理中,经常用到mysql数据库,wordpress搭建的worklog.搜索测试数据.我blog的测试环境等.我在自己的电脑上整了WAMP(Windows Apache MySQL P ...
- E. Karen and Supermarket
E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- 遍历List集合的方式
遍历List集合的方式有很多,现在举出最常见的三种 List<String> list = new ArrayList<String>(); list.add("a& ...
- HBuilder 做移动端app流程
1.新建一个移动项目 2.编写代码 3.发行-发行为原生安装包,配置参数 选择icon 和引导页
- 假如m是奇数,且m>=3,证明m(m² -1)能被8整除
m是奇数,且m>=3 =>m可以用表达式2n-1,n>=2 =>m²-1 = (2n-1)²-1 =>m²-1 = 4n²-4n+1-1 =>m²-1 = 4n²- ...
- hdfs校验和
hdfs完整性:用户希望储存和处理数据的时候,不会有任何损失或者损坏.所以提供了两种校验: 1.校验和(常用循环冗余校验CRC-32). 2.运行后台进程来检测数据块. 校验和: a.写入数据节点验证 ...
- js进阶之闭包
1.首先说下什么是闭包,闭:封闭,包:就像我们的包包~:虽然我在扯淡但是也有其道理咯,闭包就有点像java里面的封装一样,包属性和方法都封装到对象内部,在外部通过共有的get.set方法获取或者设置其 ...