剑指Offer数据结构之栈和队列[Python版]
面试题005 用两个栈实现队列
题目表述:
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
解题思路:
实现入队和出队操作,stackA用来进栈,stackB出栈,stackB为空则stackA出栈给stackB
代码:
# -*- coding:utf-8 -*- class Solution: def __init__(self): self.stackA = [] self.stackB = [] def push(self, node): self.stackA.append(node) def pop(self): if not self.stackB: while self.stackA: self.stackB.append(self.stackA.pop()) return self.stackB.pop()
相关推荐
koushr 2020-11-12
zhangxiafll 2020-11-13
kikaylee 2020-10-31
范范 2020-10-28
MILemon 2020-10-22
hugebawu 2020-10-12
LauraRan 2020-09-28
shenwenjie 2020-09-24
omyrobin 2020-09-23
guangcheng 2020-09-22
qiangde 2020-09-13
hanyujianke 2020-08-18
晨曦之星 2020-08-14
xiesheng 2020-08-06
KAIrving 2020-08-02
xiesheng 2020-08-02
范范 2020-07-30
chenfei0 2020-07-30