1.思路
来回倒
- 一个栈管
入队
功能,一个栈管出队
功能 没出队
的元素要先重新
放到管入队
的栈中
2.代码
import java.util.Stack;
public class Solution {
Stack<Integer> in = new Stack<Integer>();//始终用于入队
Stack<Integer> out = new Stack<Integer>();//始终用于出队
public void push(int node) {
while(!out.isEmpty()){
in.push(out.pop());
}//没出完就再入栈
in.push(node);
}
public int pop() {
while(!in.isEmpty()){
out.push(in.pop());
}//入队的栈的所有数据都要移到出队的栈中,因为最先进的数据在栈的最底部
return out.pop();
}
}