题解 | #用两个栈实现队列#
用两个栈实现队列
http://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6
A栈只管进,B栈只管出,B栈没有数据时,把A栈的全部拿过来再出
static Stack<integer> stack1 = new Stack<integer>();
static Stack<integer> stack2 = new Stack<integer>();</integer></integer></integer></integer>
public static void push(int node) {
stack2.push(node);
}
public static int pop() {
if(stack1.isEmpty()){
int size = stack2.size();
for (int i = 0; i < size; i++) {
stack1.push(stack2.pop());
}
}
return stack1.pop();
}
基恩士成长空间 423人发布