天天看點

JZ5 用兩個棧實作隊列 pythonJZ5 用兩個棧實作隊列

JZ5 用兩個棧實作隊列

JZ5 用兩個棧實作隊列 pythonJZ5 用兩個棧實作隊列

my version

class Solution:
    def __init__(self):
        self.stack1 = []
        self.stack2 = []
    def push(self, node):
        self.stack1.append(node)
    def pop(self):
        if len(self.stack2) == 0:
            while self.stack1:
                self.stack2.append(self.stack1.pop())
        return self.stack2.pop()
           

python中list自帶.pop(), push操作相當于.append()