Python | Leetcode Python题解之第225题用队列实现栈

题目:

题解:

class MyStack:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.queue = collections.deque()


    def push(self, x: int) -> None:
        """
        Push element x onto stack.
        """
        n = len(self.queue)
        self.queue.append(x)
        for _ in range(n):
            self.queue.append(self.queue.popleft())


    def pop(self) -> int:
        """
        Removes the element on top of the stack and returns that element.
        """
        return self.queue.popleft()


    def top(self) -> int:
        """
        Get the top element.
        """
        return self.queue[0]


    def empty(self) -> bool:
        """
        Returns whether the stack is empty.
        """
        return not self.queue

相关推荐

  1. [力扣题解]225. 队列实现

    2024-07-11 09:54:01       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 09:54:01       101 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 09:54:01       108 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 09:54:01       91 阅读
  4. Python语言-面向对象

    2024-07-11 09:54:01       98 阅读

热门阅读

  1. 分析一下多方联合计算中的数据泄露场景

    2024-07-11 09:54:01       24 阅读
  2. VSCode,请打开文件始终在新标签页打开

    2024-07-11 09:54:01       27 阅读
  3. JIRA的高级搜索JIRA Query Language(JQL)详解

    2024-07-11 09:54:01       30 阅读
  4. 开源项目有哪些机遇与挑战?

    2024-07-11 09:54:01       21 阅读
  5. 多器官功能障碍综合征

    2024-07-11 09:54:01       25 阅读
  6. ABAP中预制会计凭证的BAPI使用方法

    2024-07-11 09:54:01       26 阅读
  7. 力扣题解( 最长湍流子数组)

    2024-07-11 09:54:01       26 阅读
  8. ORACLE 数据库ADG切换

    2024-07-11 09:54:01       24 阅读
  9. Memcached介绍和详解

    2024-07-11 09:54:01       28 阅读
  10. js实现打印

    2024-07-11 09:54:01       28 阅读
  11. dlib简介

    2024-07-11 09:54:01       30 阅读
  12. PDF文件在线处理工具(侧重数理及论文)

    2024-07-11 09:54:01       21 阅读
  13. 20.js获取页面卷去的距离以及滚到到指定位置

    2024-07-11 09:54:01       28 阅读
  14. 【人脸识别、Python实现】PyQt5人脸识别管理系统

    2024-07-11 09:54:01       25 阅读