剑指offer 两个栈实现队列 解法错误修正
push的上一个操作如果是pop,数据全部在out栈,需要全部压入in栈再push新的数才能保证push到队尾
This commit is contained in:
@ -411,6 +411,9 @@ Stack<Integer> in = new Stack<Integer>();
|
||||
Stack<Integer> out = new Stack<Integer>();
|
||||
|
||||
public void push(int node) {
|
||||
if (in.isEmpty())
|
||||
while (!out.isEmpty())
|
||||
in.push(out.pop());
|
||||
in.push(node);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user