232. Implement Queue using Stacks (用栈实现队列)
来源:互联网 发布:如何看到淘宝追加评论 编辑:程序博客网 时间:2024/06/06 15:50
Implement the following operations of a queue using stacks.
- push(x) -- Push element x to the back of queue.
- pop() -- Removes the element from in front of queue.
- peek() -- Get the front element.
- empty() -- Return whether the queue is empty.
- You must use only standard operations of a stack -- which means only
push to top
,peek/pop from top
,size
, andis empty
operations are valid. - Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
class MyQueue {private LinkedList<Integer> stack1 = new LinkedList<Integer>(),stack2 = new LinkedList<Integer>();// Push element x to the back of queue.public void push(int x) {stack1.addFirst(x);}// Removes the element from in front of queue.public void pop() {if (stack2.isEmpty()) {for (int i : stack1)stack2.addFirst(i);stack1.clear();}stack2.removeFirst();}// Get the front element.public int peek() {if (stack2.isEmpty()) {for (int i : stack1)stack2.addFirst(i);stack1.clear();}return stack2.getFirst();}// Return whether the queue is empty.public boolean empty() {return stack1.isEmpty() && stack2.isEmpty();}}
0 0
- 232. Implement Queue using Stacks (用栈实现队列)
- 队列-Implement Queue using Stacks(用栈实现队列)
- 232.Implement Queue using Stacks(用栈实现队列)
- Implement Queue using Stacks(用栈实现队列)
- LeetCode 232. Implement Queue using Stacks(用栈实现队列)
- leetcode 232. Implement Queue using Stacks 双栈实现队列
- LeetCode(23)-Implement Queue using Stacks(栈实现队列)
- 232. Implement Queue using Stacks | 用栈实现队列的功能
- Implement Queue using Stacks 用俩栈实现队列
- 232. Implement Queue using Stacks (栈)
- Implement Queue using Stacks 两个栈实现队列
- Leetcode232. Implement Queue using Stacks[栈实现队列]
- LeetCode Implement Queue using Stacks 栈&队列
- leetcode 232. Implement Queue using Stacks-栈模拟队列
- LeetCode学习之-232. 利用堆栈实现队列(Implement Queue using Stacks)
- leetcode:用两个栈实现一个队列 Implement Queue using Stacks
- LeetCode OJ 之 Implement Queue using Stacks(使用栈实现队列)
- [LeetCode-232] Implement Queue using Stacks(两个栈实现一个队列)
- AsyncTask源码阅读笔记
- 常见组件对象的搞法
- ZMQ Version3.2.6基础介绍
- 如何把andorid5.1的source中的framework的class提供给android工程师调试
- 最短路算法
- 232. Implement Queue using Stacks (用栈实现队列)
- 离散化操作
- android UEventObserver的用法
- synchronized 中 intern 用法
- poj 3040
- js验证表单输入框
- python django1.10解决view must be a callable list/tuple in case of include()
- springMVC两种方式实现多文件上传及效率比较
- NOIP 2013 CODE[VS] 3287 货车运输 倍增LCA + 最大生成树