Implement Stack using Queues
Implement the following operations of a stack using queues.
- 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 empty
operations 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).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
class Stack {
private:
queue <int> q_1;
queue <int> q_2;
public:
// Push element x onto stack.
void push(int x) {
if(q_1.empty())
q_1.push(x);
else
{
while(!q_1.empty())
{
q_2.push(q_1.front());
q_1.pop();
}
q_1.push(x);
while(!q_2.empty())
{
q_1.push(q_2.front());
q_2.pop();
}
}
} // Removes the element on top of the stack.
void pop() {
q_1.pop();
} // Get the top element.
int top() {
return q_1.front();
} // Return whether the stack is empty.
bool empty() {
return q_1.empty();
}
};
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) -- ...
- [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 ...
- (leetcode)Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- Android实战--电话拨号器
今天跟着黑马视频建立一个android app--电话拨号器 首先新建一个android项目 activity_main_xml中的代码如下: <RelativeLayout xmlns:and ...
- C语言-03-流程控制
一.选择结构 1> if语句 使用注意 ① if语句中的条件语句,不要把==和=弄混,可以把常量作为左值, 这样的话,在无用=的情况下,编译时会报错 ② if语句后若要定义新的变量或者有多条语句 ...
- js地理位置获取、显示、轨迹绘制
JS新API标准 地理定位(navigator.geolocation) 基于 html5 geolocation来获取经纬度地址 Html5 Geolocation获取地理位置信息 HTML5获取地 ...
- openswitch db files
http://openvswitch.org/support/dist-docs/ovsdb-tool.1.html FILES The default db is /etc/openvswitch/ ...
- javascript 如何访问 action或者controller 传给 jsp 页面的值
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- cookie的读入和读出
写入cookie中 在mvc的控制器中 HttpCookie GetUserID = new HttpCookie("uID", 要保存的值); GetUserID.Expires ...
- matlab2015b调用摄像头
参考链接:http://blog.csdn.net/lyqmath/article/details/7307429 本人电脑是宏碁T5000 调用代码: % By lyqmathclc; clear ...
- 数据结构--栈的应用(表达式求值 nyoj 35)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35 题目: 表达式求值 时间限制:3000 ms | 内存限制:65535 KB描述 AC ...
- 第九章:四大组件之Broadcast Receiver
第九章:四大组件之Broadcast Receiver 一.广播的功能和特征 广播的生命周期很短,经过调用对象-->实现onReceive-->结束,整个过程就结束了.从实现的复杂度和 ...
- Canvas修行之黑客帝国代码雨
既然是修行,不卖弄关子,不吊胃口,修行成果必须先晒一晒. 下图是我用canvas画的黑客帝国代码雨,想起当年看黑客帝国时,那个代码雨场景让我心旷神怡,大开脑洞,满脑子是那种三维空间,无数0和1像雨一样 ...