首页 > 其他分享 >232. Implement Queue using Stacks

232. Implement Queue using Stacks

时间:2022-11-06 11:22:05浏览次数:46  
标签:return helper self pop Queue ._ using Implement data

push:
直接插入辅助栈

pop/peak:
1.如果数据栈有数据,直接出栈;
2.1.否则,如果辅助栈有数据,不停出栈给到数据栈
2.2.出栈数据栈中的元素

class MyQueue:
    def __init__(self):
        self._data = []
        self._helper = []

    def push(self, x: int) -> None:
        self._data.append(x)

    def pop(self) -> int:
        if self._helper:
            return self._helper.pop()
        while len(self._data) > 0:
            self._helper.append(self._data.pop())
        return self._helper.pop()

    def peek(self) -> int:
        if self._helper:
            return self._helper[-1]
        while len(self._data) > 0:
            self._helper.append(self._data.pop())
        return self._helper[-1]

    def empty(self) -> bool:
        return len(self._data) == 0 and len(self._helper) == 0

标签:return,helper,self,pop,Queue,._,using,Implement,data
From: https://www.cnblogs.com/zijidan/p/16862240.html

相关文章

  • 阻塞队列 - BlockingQueue
     线程通信的一个工具。在任意时刻,不管并发有多高,在单JVM上面,同一时间永远只有一个线程能够对队列进行入队或者出队操作。1.线程安全的队列;2.队列类型:无限队列、有限队......
  • C++——优先级队列(priority_queue)
    其为队列需要包含头文件#include,其与queue的不同之处在于,可以自定义数据的优先级,让优先级高的排在队列的前面,优先出队;优先队列具有队列的所有特性,包括基本操作,只是在此基......
  • JAVA并发容器-ConcurrentLinkedQueue 源码分析
    在并发编程中,有时候需要使用线程安全的队列。如果要实现一个线程安全的队列有两种方式:一种是使用阻塞算法,另一种是使用非阻塞算法。使用阻塞算法的队列可以用一个锁(入队和......
  • Negotiating Team Formation Using Deep Reinforcement Learning
    背景:多个智能体在环境中交互时(主要是模仿人类的团队行为例如篮球比赛等,只有通过合作才能够达到一定的目的),通常是需要合作从而实现最终目标,即达到最佳总收益。一般来说,实......
  • 406.queue-reconstruction-by-height 根据身高重建队列
    问题描述406.根据身高重建队列解题思路首先根据身高对数组重新排序,再根据ki进行插入操作。排序时,需要对排序的比较方法重写,参见C++sort排序函数用法。同时,考虑到基于......
  • Polygon Clipping using DotProduct
    写在前面:本文章为个人学习笔记,方便以后自己复习,也希望能帮助到他人。由于本人水平有限难免出现错误,还请评论区指出,多多指教。部分图元和素材来源于网络,如有侵权请联系本......
  • [单片机框架] [queue] 实现一个简易的消息队列
    使用方法如下:#defineUSB_RECV_Q_ITEM_CNT8#defineUSB_RECV_Q_ITEM_SIZE(64+1)//用于usb消息队列总缓存区......
  • ThreadPoolExecutor BlockingQueue讲解
    有四种常用阻塞队列策略:1.直接拒绝:(DirectHandoffs)一个好的工作队列应该是不缓存任务,而是直接交给线程处理,就如SynchronousQueue一样。一个任务将会入队失败,如果没有......
  • calico 报auto-detect an IPv4 address using interface regexes [ens18]: no valid h
    现象:.查看calicopod的时候报auto-detectanIPv4addressusinginterfaceregexes[ens18]:novalidhostinterfacesfound 分析:calico开启了ipvs地址自动检测......
  • NISACTF2022--join-us(join using 无列名注入)
    join-us--join-using无列名查询访问登录页面,看到一个catugetflag输入框试试sql注入有报错信息  黑名单byupdatexmldatabaseunioncolumns=substr......