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:

  1. MyQueue queue = new MyQueue();
  2.  
  3. queue.push(1);
  4. queue.push(2);
  5. queue.peek(); // returns 1
  6. queue.pop(); // returns 1
  7. 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).
  1. class MyQueue:
  2.  
  3. def __init__(self):
  4. """
  5. Initialize your data structure here.
  6. """
  7. self.stack_in, self.stack_out = [], []
  8.  
  9. def push(self, x: int) -> None:
  10. """
  11. Push element x to the back of queue.
  12. """
  13. self.stack_in.append(x)
  14.  
  15. def pop(self) -> int:
  16. """
  17. Removes the element from in front of queue and returns that element.
  18. """
  19. self._transfer()
  20. return self.stack_out.pop()
  21.  
  22. def peek(self) -> int:
  23. """
  24. Get the front element.
  25. """
  26. self._transfer()
  27. return self.stack_out[-1]
  28.  
  29. def empty(self) -> bool:
  30. """
  31. Returns whether the queue is empty.
  32. """
  33. return not self.stack_in and not self.stack_out
  34.  
  35. def _transfer(self):
  36. if not self.stack_out:
  37. while self.stack_in:
  38. self.stack_out.append(self.stack_in.pop())
  39.  
  40. # Your MyQueue object will be instantiated and called as such:
  41. # obj = MyQueue()
  42. # obj.push(x)
  43. # param_2 = obj.pop()
  44. # param_3 = obj.peek()
  45. # 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. JavaScript学习总结(七)

    这一讲我们来学习DOM编程(十分重要),有了DOM编程,我们就可以操作任意的HTML元素了. DOM,文档对象模型 一个html页面被浏览器加载的时候,浏览器就会对整个html页面上的所有标签都会创建 ...

  2. Mybatis学习——Mybatis核心配置

    MyBatis的核心配置 在使用MyBatis框架时,设计两个核心的d对象:SqlSessionFactory和SqlSession. SqlsessionFactory SqlSessionFact ...

  3. Centos7.6部署rsyslog+loganalyzer+mysql日志管理服务器

    参考来自: the_script :https://yq.aliyun.com/articles/675198 名山.深处:https://www.cnblogs.com/skychenjiajun/ ...

  4. c#之初识结构(Struct)

    C# 结构(Struct) 首先结构是值类型数据结构.它使得一个单一变量可以存储各种数据类型的相关数据.struct 关键字用于创建结构.通俗说:结构就是一个可以包含不同数据类型的集合.它是一种可以自 ...

  5. MyBatis从入门到精通(第9章):Spring集成MyBatis(上)

    MyBatis从入门到精通(第9章):Spring集成MyBatis(上) Spring是一个为了解决企业级Web应用开发过程中面临的复杂性,而被创建的一个非常流行的轻量级框架. mybatis-sp ...

  6. Windows Boot Manager、Bootmgfw.efi、Bootx64.efi、bcdboot.exe 文件的关系

    本教程针对于UEFI启动来叙述的,根据普遍的支持UEFI的机器来叙述. 标题简要说明:Windows Boot Manager  --------安装完Windows系统后而出现的启动选项(相关的信息 ...

  7. CodeForces 996B World Cup(思维)

    https://codeforces.com/problemset/problem/996/B 题意: 圆形球场有n个门,Allen想要进去看比赛.Allen采取以下方案进入球场:开始Allen站在第 ...

  8. java.lang.AbstractMethodError: org.slf4j.impl.JDK14LoggerAdapter.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V

    java.lang.AbstractMethodError: org.slf4j.impl.JDK14LoggerAdapter.log(Lorg/slf4j/Marker;Ljava/lang/St ...

  9. springboot学习笔记:9.springboot+mybatis+通用mapper+多数据源

    本文承接上一篇文章:springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+fre ...

  10. xcode6添加pch文件

    pch文件 定义:该文件中定义的内容为全局变量,可供所有类进行调用 例子:在pch文件中定义ios版本