题解 | #用两个栈实现队列#
用两个栈实现队列
http://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6
本文仅用于记录
简单题,15分钟才做出来,懒得优化了。。。
跟倒豆子一般。
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
while(stack1.isEmpty() == false){
stack2.push(stack1.pop());
}
int result = stack2.pop();
while(stack2.isEmpty() == false){
stack1.push(stack2.pop());
}
return result;
}
}

查看15道真题和解析