index
title: 用两个栈实现一个队列 date: 2019-08-21T11:00:41+08:00 draft: false categories: offer
题目
解题思路
Stack<Integer> pushStack = new Stack<>();
Stack<Integer> popStack = new Stack<>();
public void push(int node) {
pushStack.push(node);
}
public int pop() {
if (popStack.isEmpty()) {
while (!pushStack.isEmpty()) {
popStack.push(pushStack.pop());
}
}
if (popStack.isEmpty()) return -1;
else return popStack.pop();
}Last updated