解法
首先知道 栈和队列的特点
栈:先进后出
队列:先进先出
如果用栈实现的话 可以知道 每次弹出的都是栈底的元素,所以我们可以把元素都倒在另外一个栈中,然后在弹出栈底的 最后在倒回来。
运行时间:12ms 占用内存:9428KB
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() {
while(!stack1.empty()){
stack2.push(stack1.pop());
}
int ans = stack2.pop();
while(!stack2.empty()){
stack1.push(stack2.pop());
}
return ans;
}
} 
京公网安备 11010502036488号