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. Unity3D Shader描边效果

    Shader "Custom/RimColor" { Properties { _MainTex ("Base (RGB)", 2D) = "whit ...

  2. 编译安装的gitlab8.x如何修改时区设置

    编译安装的gitlab 8.x版本默认的时区是UTC,在页面上显示的时间默认是零时区的区时,安装完成之后,如果页面上显示的时间比北京时间少了8个小时,则需要修改一下时区 把gitlab.yml文件中的 ...

  3. tornado web开发

      tornado是python的web框架,这里简单记录下利用tornado怎么实现文件的上传,其中web.py上传功能类似. 直接用代码说明: 代码来自:http://my.oschina.net ...

  4. css如何设置div中的内容垂直居中?

    <style>.out { position: relative;//相对div的定位 top: 30%;//相对div的border-top的距离,根据元素的高度,50%则为垂直居中:} ...

  5. numpy协方差矩阵numpy.cov

    numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None)[source] Estim ...

  6. 9.3Django

    2018-9-3 13:56:18 开始进行Django!!!! 2018-9-3 14:48:25 出去玩去了!!啦啦啦! Django还是很好玩的! 贴上笔记 day60 2018-04-27 1 ...

  7. Mysql----数据备份、pymysql模块

    一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https://pan.baidu.com/s/1bpo5mqj 掌握: #1. 测试+链接 ...

  8. .Net微服务架构之运行日志分析系统

    一.引言 .Net技术栈目前还没有像spring cloud相对完整一整微服务架构栈,随着业务发展系统架构演进,自行构建.Net技术体系的微服务架构,配套相关核心组件.因平台基于微服务架构方式研发,每 ...

  9. POJ 1984 - Navigation Nightmare - [带权并查集]

    题目链接:http://poj.org/problem?id=1984 Time Limit: 2000MS Memory Limit: 30000K Case Time Limit: 1000MS ...

  10. codeforces 792D - Paths in a Complete Binary Tree

    #include<cstdio> #include<iostream> #define lowbit(x) x&(-x) typedef long long ll; u ...