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 backpeek/pop from frontsize, 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).

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.

解析:java 中,Queue接口可使用的函数有 offer(E e):添加到队列尾  peek():查看队列头  poll():删除队头元素 remove()移除队头元素

另外:Deque接口中的push(),pop()操作均是在队头操作。

代码如下:

class MyStack {
// Push element x onto stack.
Queue<Integer> q1=new LinkedList<Integer>();
Queue<Integer> q2=new LinkedList<Integer>(); public void push(int x) {
q1.offer(x);
} // Removes the element on top of the stack.
public void pop() {
while(q1.size()>1) q2.offer(q1.poll());
q1.poll();
Queue<Integer> q=q1;
q1=q2;
q2=q; } // Get the top element.
public int top() {
while(q1.size()>1) q2.offer(q1.poll());
int x=q1.poll();
q2.offer(x);
Queue<Integer>q=q1;
q1=q2;
q2=q;
return x;
} // Return whether the stack is empty.
public boolean empty() {
return q1.isEmpty();
}
}

  运行结果:

(easy)LeetCode 225.Implement Stack using Queues的更多相关文章

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

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

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

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

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

  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. Java [Leetcode 225]Implement Stack using Queues

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

  6. Leetcode 225 Implement Stack using Queues STL

    用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...

  7. 【easy】225. Implement Stack using Queues

    用队列实现栈.这个实现方法十分的简单,就是在push这一步的时候直接变成逆序. class MyStack { private: queue<int> q; queue<int> ...

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

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

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

随机推荐

  1. python3_RoboBrowser_test

    python3_RoboBrowser_test selenium库作为交互是非常方便的,但是却大大加长了加载的时间,例如需要渲染网址,加载js,造成在爬虫过程中时间变长. 因此找到一个虚拟的浏览器, ...

  2. html之table标签

    简单的html表格,由table元素以及一个或多个tr,th,td元素组成. tr:定义表格行 th:定义表格头 td:定义表格单元 更复杂的 HTML 表格也可能包括 caption.col.col ...

  3. FAQ unzip无法解压文件

    [root@TEST144239 tmp]# unzip linx64_11gR2_database_1of2.zipArchive:  linx64_11gR2_database_1of2.zipw ...

  4. rm命令

    rm是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比如在/(根目录)下执行rm * -rf).所以,我们在执行rm之前最好先确认一下在哪个目录,到底要删除什么东西 ...

  5. UI-UIImageView和Image的区别

    1.UIImageView图片视图控件 继承于UIView 用于显示图片在应用程序中 2.UIImage 是将真实图片文件转化为程序中的图片,然后3.UIImageView是Image的载体,负责显示 ...

  6. Js闭包函数

    一.变量的作用域要理解闭包,首先必须理解Javascript特殊的变量作用域.变量的作用域无非就是两种:全局变量和局部变量.Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量. ( ...

  7. 关于git配合tortoiseGit的基础使用

    一定要自己写出来才能牢记,所以我来写一下 git确实比svn好用的多了,最起码只有一个文件夹用来标记版本信息比svn所有文件夹下都要放一个文件夹来标记版本信息先进多了,不然你不想要版本管理这些文件的时 ...

  8. 【MySQL】insert批量插入优化方案

    对于一些数据量较大的系统,数据库面临的问题除了查询效率低下,还有就是数据入库时间长.特别像报表系统,每天花费在数据导入上的时间可能会长达几个小时或十几个小时之久.因此,优化数据库插入性能是很有意义的. ...

  9. 黄聪:jquery mobile使用form进行post提交表单没有反应,显示空白页解决方案

    jquery mobile这货会自动用Ajax方式. 所以需要在表单form标签添加data-ajax="false"这个元素. <form method="pos ...

  10. Form_Form标准控件Folder开发解析(案列)

    2014-01-09 Created By BaoXinjian 1. 打开APPSTAND.fmb, 并加载程序库APPFLDR.pll. 2. 基于APPSTAND.fmb生成Folder开发所需 ...