思路:
用户给我数据,只进 push 栈,用户给我数据,永远只进从 pop 栈拿。
但是有两个限制,
1. 如果 push 栈要往 pop 栈里倒数据,一次要倒完。
2. 如果 pop 栈里有东西,push 栈里一定不要倒!
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() {
if (stack2.size() <= 0) {
while (stack1.size() != 0) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}