Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

Example:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false

Notes:

  • You must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
class MyQueue:

    def __init__(self):
"""
Initialize your data structure here.
"""
self.stack_in, self.stack_out = [], [] def push(self, x: int) -> None:
"""
Push element x to the back of queue.
"""
self.stack_in.append(x) def pop(self) -> int:
"""
Removes the element from in front of queue and returns that element.
"""
self._transfer()
return self.stack_out.pop() def peek(self) -> int:
"""
Get the front element.
"""
self._transfer()
return self.stack_out[-1] def empty(self) -> bool:
"""
Returns whether the queue is empty.
"""
return not self.stack_in and not self.stack_out def _transfer(self):
if not self.stack_out:
while self.stack_in:
self.stack_out.append(self.stack_in.pop()) # Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()

[LC] 232. Implement Queue using Stacks的更多相关文章

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

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

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

  3. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  4. 232. Implement Queue using Stacks

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

  5. (easy)LeetCode 232.Implement Queue using Stacks

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

  6. Java [Leetcode 232]Implement Queue using Stacks

    题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...

  7. LeetCode 232 Implement Queue using Stacks

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

  8. 【LeetCode】232. Implement Queue using Stacks

    题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...

  9. 【一天一道LeetCode】#232. Implement Queue using Stacks

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...

随机推荐

  1. [De1CTF 2019]SSRF Me-MD5长度扩展攻击&CVE-2019-9948

    0x00 打开题目查看源代码,开始审计 这里贴上网上师傅的博客笔记: https://xz.aliyun.com/t/6050 #! /usr/bin/env python #encoding=utf ...

  2. Python 字典列表嵌套输入问题

  3. CMake常用变量

    CMake变量 CMake共用七种变量,如下所示: 目录: ()提供信息的变量. ()控制变量. ()描述系统的变量. ()控制构建过程的变量. ()语言变量. ()CTest变量. (7)CPack ...

  4. Python—后台运行(nohup 、&、 2>&1详解)

    一.脚本文件(test.py) # -*- coding: UTF-8 -*- import time print("hello"," python") os. ...

  5. order by rand()优化

    优化前: SELECT id, loan_id, NAME, company FROM tablename WHERE time BETWEEN 1522512000 AND 1525103999 A ...

  6. Struts 2 的常规配置

    Struts 2 的默认配置文件是struts.xml,该文件应该放在Web应用的类加载路径下,通常就是放在WEB-INF/classes路径下. struts.xml文件的最大作用是配置Action ...

  7. 求素数的一个快速算法 Python 快速输出素数算法

    思想 以100以内为例. 生成一个全是True的101大小的数组 2开始,遇到2的倍数(4,6,8,10...)都赋值为False 因为这些数字都有因子 2 3开始,遇到3的倍数(6,9,12...) ...

  8. flask框架-下

    Local与偏函数 threasing.local 多个线程修改同一个数据,复制多份变量给每个线程用,为每个线程开辟一块空间进行数据存储. 不使用therading.local # 不用local f ...

  9. 配对t检验

  10. Java--Json解析

    普通Json {"code":"S0000", "describe":"数据正常返回", "result&qu ...