232 Implement Queue using Stacks
232 Implement Queue using Stacks
用2个stack 完成 代码如下
class Queue: # initialize your data structure here. def __init__(self): self.stackA = [] self.stackB = [] # @param x, an integer # @return nothing def push(self, x): self.stackA.append(x) # @return nothing def pop(self): while self.stackA != []: self.stackB.append(self.stackA.pop()) self.stackB.pop() while self.stackB != []: self.stackA.append(self.stackB.pop()) # @return an integer def peek(self): while self.stackA != []: self.stackB.append(self.stackA.pop()) x = self.stackB[-1] while self.stackB != []: self.stackA.append(self.stackB.pop()) return x # @return an boolean def empty(self): return self.stackA == []