0
点赞
收藏
分享

微信扫一扫

【Leetcode-剑指Offer】剑指 Offer 09. 用两个栈实现队列 (Python)

月半小夜曲_ 2022-03-19 阅读 57
python

1 题目

在这里插入图片描述
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof

2 解题思路

栈:数据是先进后出;
队列:数据是先进先出。
来源:https://www.bilibili.com/video/BV1CK411c7gx?p=6
在这里插入图片描述

3 代码实现

class CQueue(object):

    def __init__(self):
        self.stack1 = []
        self.stack2 = []

    def appendTail(self, value):
        """
        :type value: int
        :rtype: None
        """
        self.stack1.append(value)

    def deleteHead(self):
        """
        :rtype: int
        """
        if self.stack2 ==[]:
            while self.stack1 !=[]:
                self.stack2.append(self.stack1.pop())
        
        if self.stack2 !=[]:
            return self.stack2.pop()
        
        else:
            return -1

举报

相关推荐

【剑指offer】09.用两个栈实现队列

0 条评论