您的位置:首页 > 产品设计 > UI/UE

232. Implement Queue using Stacks

2016-06-08 11:40 363 查看
题目:https://leetcode.com/problems/implement-queue-using-stacks/

代码:

class MyQueue {
Queue<Integer> stack = new LinkedList<>();
// Push element x to the back of queue.
public void push(int x) {
Queue<Integer> temp = new LinkedList<>();
while(!stack.isEmpty())
{
temp.add(stack.poll());
}
temp.add(x);
while(!temp.isEmpty())
{
stack.add(temp.poll());
}
}
// Removes the element from in front of queue.
public void pop() {
stack.poll();
}
// Get the front element.
public int peek() {
return stack.peek();
}
// Return whether the queue is empty.
public boolean empty() {
return stack.isEmpty();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: