用两个栈实现队列

2018-03-30  本文已影响0人  GoDeep

题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

# -*- coding:utf-8 -*-
class Solution:
    s1, s2 = [], []
    def push(self, node):
        # write code here
        self.s1.append(node)
        
    def pop(self):
        # return xx
        while self.s1: self.s2.append(self.s1.pop())
        v = self.s2.pop()
        while self.s2: self.s1.append(self.s2.pop())
        return v
上一篇下一篇

猜你喜欢

热点阅读