NC76-两个栈实现队列(Python)
用两个栈实现队列
http://www.nowcoder.com/questionTerminal/54275ddae22f475981afa2244dd448c6
好好用Python实现,大家用Python实现的时候因为Python不像C++/Java有这个类。便用list()随意代替了,感觉不可取。所以对原结构全方位模拟了一遍。
class Solution: class Stack: def __init__(self): self.stack = [] def get(self): return self.stack def push(self, node): self.stack.append(node) def pop(self): if len(self.stack) == 0: raise IndexError return self.stack.pop() def len(self): return len(self.stack) def empty(self): self.stack = [] def __init__(self): self.stack_1 = self.Stack() self.stack_2 = self.Stack() def get(self): return self.stack_1.get() def push(self, node): # write code here self.stack_1.push(node) return self.get() def pop(self): # return xx if self.stack_1.len() == 0: raise IndexError while self.stack_1.len(): self.stack_2.push(self.stack_1.pop()) re = self.stack_2.pop() while self.stack_2.len(): self.stack_1.push(self.stack_2.pop()) return re