考察 栈
本题要点: 只有输出栈空的时候,才允许将输入栈的数据pop过去。
逻辑问题!! 想明白了。
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());
}
}
return stack2.pop();
}
}
京公网安备 11010502036488号