題目描述
用兩個棧來實作一個隊列,完成隊列的Push和Pop操作。 隊列中的元素為int類型。
解題思路
-
棧1 用來作入隊列
棧2 用來出隊列,當棧2為空時,棧1全部出棧到棧2,棧2再出棧(即出隊列)
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.st1 = []
self.st2 = []
def push(self, node):
# write code here
self.st1.append(node)
def pop(self):
# return xx
if self.st2==[]:
for i in range(len(self.st1)):
self.st2.append(self.st1.pop())
return self.st2.pop()