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.

Example:

MyStack stack = new MyStack();

stack.push(1);
stack.push(2);
stack.top(); // returns 2
stack.pop(); // returns 2
stack.empty(); // returns false

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

利用如下图所示的方式, 每次push的时候将queue里面的值都放到x的后面即可.

Push: O(n), others : O(1)

Code

class MyStack(object):

    def __init__(self):
"""
Initialize your data structure here.
"""
self.q = collections.deque() def push(self, x):
"""
Push element x onto stack.
:type x: int
:rtype: void
"""
size = len(self.q)
self.q.append(x)
while size:
self.q.append(self.q.popleft())
size -= 1 def pop(self):
"""
Removes the element on top of the stack and returns that element.
:rtype: int
"""
return self.q.popleft() def top(self):
"""
Get the top element.
:rtype: int
"""
return self.q[0] def empty(self):
"""
Returns whether the stack is empty.
:rtype: bool
"""
return not self.q # Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()

[LeetCode] 225. Implement Stack using Queues_Easy tag: Design的更多相关文章

  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. (easy)LeetCode 225.Implement Stack using Queues

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

  5. Leetcode 225 Implement Stack using Queues

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

  6. Java [Leetcode 225]Implement Stack using Queues

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

  7. [LeetCode] 232. Implement Queue using Stacks_Easy tag: Design

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  8. Leetcode 225 Implement Stack using Queues STL

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

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

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

随机推荐

  1. Excel 公式集

    1.  Excel 公式集 按身份证计算年龄 按日计算的 (2018/12/20)(身份证 C2): =TRUNC((DAYS360(CONCATENATE(MID(C2,7,4),"/&q ...

  2. @OneToMany、@ManyToOne以及@ManyToMany讲解

    一.一对多(@OneToMany)1.单向一对多模型假设通过一个客户实体可以获得多个地址信息.对于一对多的实体关系而言,表结构有两种设计策略,分别是外键关联和表关联.(1) 映射策略---外键关联在数 ...

  3. 【Web前端开发最佳实践系列】前端代码推荐和建议

    一.常用的前端文件的组织结构: 1.js (放置JavaScript代码) lib(放置框架JavaScript文件) custom.js 2.css(放置CSS样式代码) lib(放置框架CSS文件 ...

  4. Promise 必知必会的面试题

    Promise 想必大家都十分熟悉,想想就那么几个 api,可是你真的了解 Promise 吗?本文根据 Promise 的一些知识点总结了十道题,看看你能做对几道. 以下 promise 均指代 P ...

  5. 精品绿色便携软件 & 录制操作工具

    https://www.vtaskstudio.com/index.php  录制宏工具 https://soft.anruan.com/29821/  TinyTask V1.5 电脑版 https ...

  6. Android SDK更新8.1.0时报错

    Done loading packages.Preparing to install archivesDownloading SDK Platform Android 8.1.0, API 27, r ...

  7. Windows Server 2008 R2之三管理活动目录数据库

    活动目录数据库包括数据库文件NTDS.dit和日志文件.考虑到最佳性能,在生产环境推荐将日志文件和数据库文件在单独的硬盘驱动器中或RAID中,同时要根据网络的规模,保证磁盘上有充足的剩余空间.由于活动 ...

  8. 爬虫之requests详解

    requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...

  9. 9.8Django

    2018-9-8 14:34:38

  10. CH0101 a^b & CH0102 64位整数乘法

    大数取模的两道题. 虐狗宝典学习笔记: 两个数值执行算术运算时,以参与运算的最高数值类型为基准,与保存结果的变量类型无关.两个32位整数的成绩可能超过int类型的表示范围,但是CPU只会用一个32位寄 ...