Leetcode刷题记录——225. 用队列实现栈

在这里插入图片描述
说实话,这道题做的有点迷,不清楚哪些是能用的函数,哪些不能用。
后来发现,是自己对数据结构的特点不清楚。
注意:
栈 是 先入后出
队列 是 后入后出
因此,用队列实现栈,我们只能用的是
1、pop(0)(弹出[0])
2、append(录入至[-1])

class MyStack:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.stack = []
        self.pre = []
        self.pre2 = []
        


    def push(self, x: int) -> None:
        """
        Push element x onto stack.
        """
        #templist = [x]
        #self.stack = templist + self.stack
        self.stack.append(x)
        


    def pop(self) -> int:
        """
        Removes the element on top of the stack 		and returns that element.
		"""
        self.stack = self.stack[::-1]
        res = self.stack.pop(0)
        self.stack = self.stack[::-1] 
        return res


    def top(self) -> int:
        """
        Get the top element.
        """
        print(self.stack)
        return self.stack[-1]

    def empty(self) -> bool:
        """
        Returns whether the stack is empty.
        """
        print(self.stack)
        return True if self.stack == [] else False
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值