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, and is 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).

详见:https://leetcode.com/problems/implement-stack-using-queues/description/

Java实现:

方法一:两个队列实现,始终保持一个队列为空即可

class MyStack {
private Queue<Integer> queue1;
private Queue<Integer> queue2; /** Initialize your data structure here. */
public MyStack() {
queue1=new LinkedList<Integer>();
queue2=new LinkedList<Integer>();
} /** Push element x onto stack. */
public void push(int x) {
if(queue1.isEmpty()){
queue1.offer(x);
while(!queue2.isEmpty()){
queue1.offer(queue2.poll());
}
}else if(queue2.isEmpty()){
queue2.offer(x);
while(!queue1.isEmpty()){
queue2.offer(queue1.poll());
}
}
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
if(queue1.isEmpty()){
return queue2.poll();
}else{
return queue1.poll();
}
} /** Get the top element. */
public int top() {
if(queue1.isEmpty()){
return queue2.peek();
}else{
return queue1.peek();
}
} /** Returns whether the stack is empty. */
public boolean empty() {
return queue1.isEmpty()&&queue2.isEmpty();
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/

方法二:一个队列实现栈

class MyStack {
private Queue<Integer> queue; /** Initialize your data structure here. */
public MyStack() {
queue=new LinkedList<Integer>();
} /** Push element x onto stack. */
public void push(int x) {
queue.offer(x);
for(int i=0;i<queue.size()-1;++i){
queue.offer(queue.poll());
}
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue.poll();
} /** Get the top element. */
public int top() {
return queue.peek();
} /** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/

C++实现:

方法一:两个队列实现,始终保持一个队列为空即可

class MyStack {
public:
/** Initialize your data structure here. */
MyStack() { } /** Push element x onto stack. */
void push(int x) {
if(que1.empty())
{
que1.push(x);
while(!que2.empty())
{
que1.push(que2.front());
que2.pop();
}
}
else if(que2.empty())
{
que2.push(x);
while(!que1.empty())
{
que2.push(que1.front());
que1.pop();
}
}
} /** Removes the element on top of the stack and returns that element. */
int pop() {
int val;
if(que1.empty())
{
val=que2.front();
que2.pop();
}
else if(que2.empty())
{
val=que1.front();
que1.pop();
}
return val;
} /** Get the top element. */
int top() {
if(que1.empty())
{
return que2.front();
}
else if(que2.empty())
{
return que1.front();
}
} /** Returns whether the stack is empty. */
bool empty() {
return que1.empty()&&que2.empty();
}
private:
queue<int> que1;
queue<int> que2;
}; /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* bool param_4 = obj.empty();
*/

方法二:一个队列实现栈

class MyStack {
public:
/** Initialize your data structure here. */
MyStack() { } /** Push element x onto stack. */
void push(int x) {
que.push(x);
for(int i=0;i<que.size()-1;++i)
{
que.push(que.front());
que.pop();
}
} /** Removes the element on top of the stack and returns that element. */
int pop() {
int val=que.front();
que.pop();
return val;
} /** Get the top element. */
int top() {
return que.front();
} /** Returns whether the stack is empty. */
bool empty() {
return que.empty();
}
private:
queue<int> que;
}; /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* bool param_4 = obj.empty();
*/

225 Implement Stack using Queues 队列实现栈的更多相关文章

  1. 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 ...

  2. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  3. LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

    翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...

  4. [LeetCode] 225. Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  5. 225 Implement Stack using Queues(用队列实现栈Medium)

    题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop, ...

  6. LeetCode 225 Implement Stack using Queues 用队列实现栈

    1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...

  7. 【LeetCode】225. Implement Stack using Queues

    题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...

  8. 【LeetCode】225. Implement Stack using Queues 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. (easy)LeetCode 225.Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

随机推荐

  1. ZOJ 2859 二维线段树

    思路:自己写的第二发二维线段树1A.哈哈,看来对二维的push操作比較了解了:可是还没遇到在两个线段树中同一时候进行push操作的,事实上这题我是想在x维和y维同一时候进行push操作的.可是想了好久 ...

  2. MongoDB使用入门

    1.MongoDB的安装 步骤一:下载MongoDB 下载安装包:http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.4.tgz 步骤二:设置 ...

  3. Python函数参数传递

    Python中函数参数的传递是采用传值方式,但是和C/C++有所不同 C/C++方式 void fun(int a) { a = 10; } void main() { int c =3; fun(c ...

  4. 算法导论学习之线性时间求第k小元素+堆思想求前k大元素

    对于曾经,假设要我求第k小元素.或者是求前k大元素,我可能会将元素先排序,然后就直接求出来了,可是如今有了更好的思路. 一.线性时间内求第k小元素 这个算法又是一个基于分治思想的算法. 其详细的分治思 ...

  5. Linux如何查看进程等常用命令

    1.查进程    ps命令查找与进程相关的PID号:    ps a 显示现行终端机下的所有程序,包括其他用户的程序.    ps -A 显示所有程序.    ps c 列出程序时,显示每个程序真正的 ...

  6. 基于struts环境下的jquery easyui环境搭建

    下载地址: http://download.csdn.net/detail/cyberzhaohy/7348451 加入了json包:jackson-all-1.8.5.jar,项目结构例如以下: 測 ...

  7. BAPI 关闭和删除PR

    当PR在SAP里面已不再使用时,可使用批量使用以下两个BAPI进行处理: BAPI_REQUISITION_DELETE,进行删除处理, (速度快) BAPI_PR_CHANGE,进行关闭,但不删除( ...

  8. [10.27_P2] 统计损失 (简单树形DP)

    树形DP 简单题 Description 给定一棵树,每个节点有一个值.对于一条路径,它的值为路径上所有点的值的乘积.求出树上所有路径的值的和. 注意:单个点也算一条路径. Input 第 1 行一个 ...

  9. C#使用 webBrowser 控件总结

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. RabbitMQ(三)RabbitMQ消息过期时间(TTL)

    在RabbitMQ(二)AMQP协议mandatory和immediate标志位区别中我们提到,在RabbitMQ3.0以后的版本里,去掉了immediate参数支持,要实现类似的确认功能要使用TTL ...