入栈stack1;
将栈stack1推入stack2;
stack2的出栈序列就是stack1的入栈序列;
上代码
Stack<Integer> stack1=new Stack<>();
Stack<Integer> stack2=new Stack<>();
public void push(int value) {
stack1.push(value);
}
public int pop() {
if (stack2.size() <= 0 ) {
while(stack2.size()!=0) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
Stack#pop 输出栈顶
Stack#push 入栈

京公网安备 11010502036488号