用两个栈生成队列
思路: 一个入栈,一个出栈,出栈时要看出栈有没有元素,有元素直接出栈,没有元素,需要将入栈的元素放入到出栈的栈中。
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.isEmpty()){
while (!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
if(!stack2.isEmpty()){
return stack2.pop();
}else {
return -1;
}
}
}