剑指offer9 用两个栈实现队列
比较简单,直接上代码了:
运行时间:22ms
占用内存:9268k
import java.util.Stack; public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { this.stack1.push(node); } public int pop() { if(stack2.empty()){ while(!stack1.empty()){ stack2.push(stack1.pop()); } } //if(stack2.empty()) throw new RuntimeException("queue is empty!"); return stack2.pop(); } }
最好还是检查一下,并抛出异常,把注释那句用上。

浙公网安备 33010602011771号