复习-JZ5 用两个栈实现队列
用两个栈实现队列
https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6?tpId=13&&tqId=11158&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
之前做过,还是忘了最佳思路
https://blog.nowcoder.net/n/055faef37a4f47d2a88917ca902002e3
1.其实 不用 每次都弹一遍, 只要判断 2 不为空 就可以一直弹2
2空了 再把1中的弹进来就好了
2.Stack<integer> stack2 = new Stack<integer>();</integer></integer>
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() { int res = 0; while(stack1.size() != 1){ stack2.push(stack1.pop()); } res = stack1.pop(); while(!stack2.empty()){ stack1.push(stack2.pop()); } return res; } }