232. Implement Queue using Stacks
MA: class MyQueue { Stack<Integer> simQ=new Stack<Integer>(); Stack<Integer> buffer=new Stack<Integer>(); /** Initialize your data structure here. */ public MyQueue() { } /** Push element x to the back of queue. */ public void push(int x) { simQ.push(x); } /** Removes the element from in front of queue and returns that element. */ public int pop() { while(!simQ.empty()){ buffer.push(simQ.pop()); } int temp=buffer.pop(); while(!buffer.empty()){ simQ.push(buffer.pop()); } return temp; } ...
评论
发表评论