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. 关于ExpandableListView用法的一个简单小例子

    喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到an ...

  2. jQuery Mobile_公司简介

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  3. jquery显示隐藏toggle

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. CentOS6.5 yum安装 apache+svn安装配置

    首页是关于svn的备份和还原 比较快捷的方式: 备份:svnadmin hotcopy /opt/svn/svntest/ /var/tmp/svntest_20120604 --clean-logs ...

  5. Android SDK Manager 更新失败的解决方法

    Android SDK Manager 更新失败的解决方法 原文地址 最近使用Android SDK Manager 更新Android SDK tools 发现经常更新失败,获取不到更新信息: Fe ...

  6. PHP “Warning: session_start()...”、"correct (..\..\php5\Temp) in Unknown on line 0" 的解决方法

    运行php的时候出现了一下警告: Warning: Unknown: open(D:/Program Files/php5/temp1\sess_l5b1a48m6kmb1g0t5cs33690v0, ...

  7. ubuntu双网卡bonding配置(转)

    1.安装软件 apt-get install ifenslave 2.修改配置文件 /etc/network/interfaces auto lo iface lo inet loopback ifa ...

  8. (WPF) 再议binding:点击User Control时,User Control变换颜色或做其他的处理。

    Binding 是前台UI(显示层)和后台代码(数据层)的桥梁.理论上当后台的数据变动时,显示的数据或样式应该随之而变.这些是动态的. 对于Binding的设置可以在前台Xaml,也可以在后台Code ...

  9. [jQuery].scrollTop() 函数详解

    scrollTop()函数用于设置或返回当前匹配元素相对于垂直滚动条顶部的偏移. 当一个元素的实际高度超过其显示区域的高度时,在一定的设置下,浏览器会为该元素显示相应的垂直滚动条.此时,scrollT ...

  10. DBA_Oracle Database 11g 面向 DBA 和开发人员的重要特性

     2015-01-23 Created By BaoXinjian