leetCode(37):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 toppeek/pop from top
,size
,
andis 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).
两个栈,一个作为压入栈,一个作为弹出栈。当弹出栈为空时,把压入栈中的数据依次弹出并压入到弹出栈中。
假设两者均为空,说明队列为空。
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
push_stack.push(x);
} // Removes the element from in front of queue.
void pop(void) {
if (pop_stack.empty())
{
while (!push_stack.empty())
{
pop_stack.push(push_stack.top());
push_stack.pop();
}
if (!pop_stack.empty())
pop_stack.pop();
}
else
{
pop_stack.pop();
}
} // Get the front element.
int peek(void) {
if (pop_stack.empty())
{
while (!push_stack.empty())
{
pop_stack.push(push_stack.top());
push_stack.pop();
}
if (!pop_stack.empty())
return pop_stack.top();
}
else
{
return pop_stack.top();
}
return 0;
} // Return whether the queue is empty.
bool empty(void) {
if (pop_stack.empty() && push_stack.empty())
return true;
else
return false;
} private:
stack<int> pop_stack;
stack<int> push_stack;
};
leetCode(37):Implement Queue using Stacks的更多相关文章
- 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 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- (easy)LeetCode 232.Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- 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(23)-Implement Queue using Stacks
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
- Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
随机推荐
- XML签名
英文:https://www.javacodegeeks.com/2013/10/xml-security-with-digital-signature-in-java.html 中文:http:// ...
- [TJOI2009] 战争游戏
题目背景 小R正在玩一个战争游戏.游戏地图是一个M行N列的矩阵,每个格子可能是障碍物,也可能是空地,在游戏开始时有若干支敌军分散在不同的空地格子中.每支敌军都可以从当前所在的格子移动到四个相邻的格子之 ...
- 「CTSC2018」假面
真~签到题qwq 昨天在考场上先写了个70分暴力dp,然后发现好像可以优化.因为结界技能的模型相当于要求出 对于每个物品,仅仅不选它的背包是什么.... 于是当场脑补出两种做法: 前缀和后缀背包卷积 ...
- [POI2014]Tourism
题目大意: 给定一个$n(n\le20000)$条个点,$m(m\le25000)$条边的无向图,保证图中最长路径上的点数不超过$10$.对一个点染色的代价是$w_i$.求使得每个结点都被染色或至少有 ...
- unity3d 网页游戏客户端工程构建方案
将一个项目分为两个编辑环境,一个是editor,一个是target. editor只是策划人员拖拖拽拽编辑场景,打包时程序自动将每个场景资源打包生成一个XXX.unity3d文件,并最后生成一个场景配 ...
- Ubifs Support
参考:http://processors.wiki.ti.com/index.php/UBIFS_Support#Creating_UBIFS_file_system UBIFS UBIFS may ...
- Android-Binder 简析
前言 对于Android来说,Binder的重要性怎么说都不为过.不管是我们的四大组件Activity.Service.BroadcastReceiver.ContentProvider,还是经常在应 ...
- PowerPoint在线浏览的几个方案
思路:将ppt转换成pdf.image后实现在线浏览功能 下面的解决方案均不用在服务器端安装office 一.找到一个收费的restful接口,测试可用 http://www.convertapi.c ...
- centos7 安装LNMP(php7)之php7.0 yum安装
http://www.jianshu.com/p/35f21210668a 安装过程参考上面的网址
- 4. Median of Two Sorted Arrays(topK-logk)
4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...